Skip to content

Commit

Permalink
Always report patterns more complex than mut IDENT as errors
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Nov 11, 2017
1 parent 1055bdf commit f7b4b88
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
26 changes: 16 additions & 10 deletions src/librustc_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ impl<'a> AstValidator<'a> {
}
}

fn check_decl_no_pat<ReportFn: Fn(Span)>(&self, decl: &FnDecl, report_err: ReportFn) {
fn check_decl_no_pat<ReportFn: Fn(Span, bool)>(&self, decl: &FnDecl, report_err: ReportFn) {
for arg in &decl.inputs {
match arg.pat.node {
PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), _, None) |
PatKind::Wild => {}
_ => report_err(arg.pat.span),
PatKind::Ident(BindingMode::ByValue(Mutability::Mutable), _, None) =>
report_err(arg.pat.span, true),
_ => report_err(arg.pat.span, false),
}
}
}
Expand Down Expand Up @@ -149,7 +151,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
fn visit_ty(&mut self, ty: &'a Ty) {
match ty.node {
TyKind::BareFn(ref bfty) => {
self.check_decl_no_pat(&bfty.decl, |span| {
self.check_decl_no_pat(&bfty.decl, |span, _| {
struct_span_err!(self.session, span, E0561,
"patterns aren't allowed in function pointer types").emit();
});
Expand Down Expand Up @@ -253,12 +255,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
if let TraitItemKind::Method(ref sig, ref block) = trait_item.node {
self.check_trait_fn_not_const(sig.constness);
if block.is_none() {
self.check_decl_no_pat(&sig.decl, |span| {
self.session.buffer_lint(
lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY,
trait_item.id, span,
"patterns aren't allowed in methods \
without bodies");
self.check_decl_no_pat(&sig.decl, |span, mut_ident| {
if mut_ident {
self.session.buffer_lint(
lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY,
trait_item.id, span,
"patterns aren't allowed in methods without bodies");
} else {
struct_span_err!(self.session, span, E0642,
"patterns aren't allowed in methods without bodies").emit();
}
});
}
}
Expand Down Expand Up @@ -292,7 +298,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
match fi.node {
ForeignItemKind::Fn(ref decl, _) => {
self.check_decl_no_pat(decl, |span| {
self.check_decl_no_pat(decl, |span, _| {
struct_span_err!(self.session, span, E0130,
"patterns aren't allowed in foreign function declarations")
.span_label(span, "pattern not allowed in foreign function").emit();
Expand Down
1 change: 1 addition & 0 deletions src/librustc_passes/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,5 @@ register_diagnostics! {
E0226, // only a single explicit lifetime bound is permitted
E0472, // asm! is unsupported on this target
E0561, // patterns aren't allowed in function pointer types
E0642, // patterns aren't allowed in methods without bodies
}
1 change: 0 additions & 1 deletion src/test/compile-fail/no-patterns-in-args-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ trait Tr {
fn f1(mut arg: u8); //~ ERROR patterns aren't allowed in methods without bodies
//~^ WARN was previously accepted
fn f2(&arg: u8); //~ ERROR patterns aren't allowed in methods without bodies
//~^ WARN was previously accepted
fn g1(arg: u8); // OK
fn g2(_: u8); // OK
#[allow(anonymous_parameters)]
Expand Down
3 changes: 1 addition & 2 deletions src/test/compile-fail/no-patterns-in-args-macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ mod bad_pat {
m!((bad, pat));
//~^ ERROR patterns aren't allowed in function pointer types
//~| ERROR patterns aren't allowed in foreign function declarations
//~| WARN patterns aren't allowed in methods without bodies
//~| WARN this was previously accepted
//~| ERROR patterns aren't allowed in methods without bodies
}

fn main() {}

0 comments on commit f7b4b88

Please sign in to comment.