Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a lint for lets/matches which won't do anything #11122

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub enum lint {
unnecessary_qualification,
while_true,
path_statement,
irrefutable_match_without_binding,
unrecognized_lint,
non_camel_case_types,
non_uppercase_statics,
Expand Down Expand Up @@ -167,6 +168,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
default: warn
}),

("irrefutable_match_without_binding",
LintSpec {
lint: irrefutable_match_without_binding,
desc: "irrefutable let or match without any bindings (equivalent to inner expression)",
default: allow
}),

("unrecognized_lint",
LintSpec {
lint: unrecognized_lint,
Expand Down Expand Up @@ -921,6 +929,18 @@ fn check_path_statement(cx: &Context, s: &ast::Stmt) {
}
}

fn check_irrefutable_match_without_binding(cx: &Context, sp: Span, pats: &[@ast::Pat]) {
// `pats` is one or more pattern-alternatives (i.e. pat1|pat2|pat3), *not* match arms
let mut any_bindings = false;
for pat in pats.iter() {
pat_util::pat_bindings(cx.tcx.def_map, *pat, |_,_,_,_| any_bindings = true)
}
if !any_bindings {
cx.span_lint(irrefutable_match_without_binding, sp,
"irrefutable let or match without any bindings (equivalent to inner expression)")
}
}

fn check_item_non_camel_case_types(cx: &Context, it: &ast::item) {
fn is_camel_case(cx: ty::ctxt, ident: ast::Ident) -> bool {
let ident = cx.sess.str_of(ident);
Expand Down Expand Up @@ -1319,6 +1339,9 @@ impl<'a> Visitor<()> for Context<'a> {
ast::ExprParen(expr) => if self.negated_expr_id == e.id {
self.negated_expr_id = expr.id
},
ast::ExprMatch(_, [ast::Arm { ref pats, .. }]) => {
check_irrefutable_match_without_binding(self, e.span, *pats)
},
_ => ()
};

Expand All @@ -1334,6 +1357,11 @@ impl<'a> Visitor<()> for Context<'a> {
visit::walk_expr(self, e, ());
}

fn visit_local(&mut self, l: @ast::Local, _: ()) {
check_irrefutable_match_without_binding(self, l.span, &[l.pat]);
visit::walk_local(self, l, ())
}

fn visit_stmt(&mut self, s: @ast::Stmt, _: ()) {
check_path_statement(self, s);

Expand Down
50 changes: 50 additions & 0 deletions src/test/compile-fail/lint-irrefutable-match-without-binding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[deny(irrefutable_match_without_binding)];
#[allow(unused_variable)];

fn main() {
let a = 8;
let _ = a; //~ ERROR: irrefutable let or match without any bindings (equivalent to inner expression)
let _ = 99; //~ ERROR: irrefutable let or match without any bindings (equivalent to inner expression)
let (_, _) = (1, 2); //~ ERROR: irrefutable let or match without any bindings (equivalent to inner expression)
let ((_, _), ()) = (((), ()), ()); //~ ERROR: irrefutable let or match without any bindings (equivalent to inner expression)
let () = (); //~ ERROR: irrefutable let or match without any bindings (equivalent to inner expression)
match 23 { //~ ERROR: irrefutable let or match without any bindings (equivalent to inner expression)
_ => { }
}
match (1, 2) { //~ ERROR: irrefutable let or match without any bindings (equivalent to inner expression)
(_, _) => { }
}
match false { //~ ERROR: irrefutable let or match without any bindings (equivalent to inner expression)
false|true => { }
}
match [1, 2] { //~ ERROR: irrefutable let or match without any bindings (equivalent to inner expression)
[..] => { }
}
match [1, 2] { //~ ERROR: irrefutable let or match without any bindings (equivalent to inner expression)
[_, _] => { }
}
match ~666 { //~ ERROR: irrefutable let or match without any bindings (equivalent to inner expression)
~_ => { }
}

match false { // okay: more than one arm
false => { },
_ => { }
}
match [1, 2] {
[..v] => { }
}
let ((_, (_, a)), ()) = ((1, (2, 3)), ()); // okay: has a binding
let ((_, (_, ref a)), ()) = ((1, (2, 3)), ()); // okay: has a binding
let ~ref x = ~789;
}