Skip to content

Commit f7b4b88

Browse files
committed
Always report patterns more complex than mut IDENT as errors
1 parent 1055bdf commit f7b4b88

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

src/librustc_passes/ast_validation.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ impl<'a> AstValidator<'a> {
6767
}
6868
}
6969

70-
fn check_decl_no_pat<ReportFn: Fn(Span)>(&self, decl: &FnDecl, report_err: ReportFn) {
70+
fn check_decl_no_pat<ReportFn: Fn(Span, bool)>(&self, decl: &FnDecl, report_err: ReportFn) {
7171
for arg in &decl.inputs {
7272
match arg.pat.node {
7373
PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), _, None) |
7474
PatKind::Wild => {}
75-
_ => report_err(arg.pat.span),
75+
PatKind::Ident(BindingMode::ByValue(Mutability::Mutable), _, None) =>
76+
report_err(arg.pat.span, true),
77+
_ => report_err(arg.pat.span, false),
7678
}
7779
}
7880
}
@@ -149,7 +151,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
149151
fn visit_ty(&mut self, ty: &'a Ty) {
150152
match ty.node {
151153
TyKind::BareFn(ref bfty) => {
152-
self.check_decl_no_pat(&bfty.decl, |span| {
154+
self.check_decl_no_pat(&bfty.decl, |span, _| {
153155
struct_span_err!(self.session, span, E0561,
154156
"patterns aren't allowed in function pointer types").emit();
155157
});
@@ -253,12 +255,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
253255
if let TraitItemKind::Method(ref sig, ref block) = trait_item.node {
254256
self.check_trait_fn_not_const(sig.constness);
255257
if block.is_none() {
256-
self.check_decl_no_pat(&sig.decl, |span| {
257-
self.session.buffer_lint(
258-
lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY,
259-
trait_item.id, span,
260-
"patterns aren't allowed in methods \
261-
without bodies");
258+
self.check_decl_no_pat(&sig.decl, |span, mut_ident| {
259+
if mut_ident {
260+
self.session.buffer_lint(
261+
lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY,
262+
trait_item.id, span,
263+
"patterns aren't allowed in methods without bodies");
264+
} else {
265+
struct_span_err!(self.session, span, E0642,
266+
"patterns aren't allowed in methods without bodies").emit();
267+
}
262268
});
263269
}
264270
}
@@ -292,7 +298,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
292298
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
293299
match fi.node {
294300
ForeignItemKind::Fn(ref decl, _) => {
295-
self.check_decl_no_pat(decl, |span| {
301+
self.check_decl_no_pat(decl, |span, _| {
296302
struct_span_err!(self.session, span, E0130,
297303
"patterns aren't allowed in foreign function declarations")
298304
.span_label(span, "pattern not allowed in foreign function").emit();

src/librustc_passes/diagnostics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,5 @@ register_diagnostics! {
264264
E0226, // only a single explicit lifetime bound is permitted
265265
E0472, // asm! is unsupported on this target
266266
E0561, // patterns aren't allowed in function pointer types
267+
E0642, // patterns aren't allowed in methods without bodies
267268
}

src/test/compile-fail/no-patterns-in-args-2.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ trait Tr {
1414
fn f1(mut arg: u8); //~ ERROR patterns aren't allowed in methods without bodies
1515
//~^ WARN was previously accepted
1616
fn f2(&arg: u8); //~ ERROR patterns aren't allowed in methods without bodies
17-
//~^ WARN was previously accepted
1817
fn g1(arg: u8); // OK
1918
fn g2(_: u8); // OK
2019
#[allow(anonymous_parameters)]

src/test/compile-fail/no-patterns-in-args-macro.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ mod bad_pat {
3030
m!((bad, pat));
3131
//~^ ERROR patterns aren't allowed in function pointer types
3232
//~| ERROR patterns aren't allowed in foreign function declarations
33-
//~| WARN patterns aren't allowed in methods without bodies
34-
//~| WARN this was previously accepted
33+
//~| ERROR patterns aren't allowed in methods without bodies
3534
}
3635

3736
fn main() {}

0 commit comments

Comments
 (0)