@@ -70,6 +70,7 @@ pub enum lint {
70
70
unnecessary_qualification,
71
71
while_true,
72
72
path_statement,
73
+ irrefutable_match_without_binding,
73
74
unrecognized_lint,
74
75
non_camel_case_types,
75
76
non_uppercase_statics,
@@ -167,6 +168,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
167
168
default : warn
168
169
} ) ,
169
170
171
+ ( "irrefutable_match_without_binding" ,
172
+ LintSpec {
173
+ lint : irrefutable_match_without_binding,
174
+ desc : "irrefutable let or match without any bindings (equivalent to inner expression)" ,
175
+ default : allow
176
+ } ) ,
177
+
170
178
( "unrecognized_lint" ,
171
179
LintSpec {
172
180
lint : unrecognized_lint,
@@ -921,6 +929,18 @@ fn check_path_statement(cx: &Context, s: &ast::Stmt) {
921
929
}
922
930
}
923
931
932
+ fn check_irrefutable_match_without_binding ( cx : & Context , sp : Span , pats : & [ @ast:: Pat ] ) {
933
+ // `pats` is one or more pattern-alternatives (i.e. pat1|pat2|pat3), *not* match arms
934
+ let mut any_bindings = false ;
935
+ for pat in pats. iter ( ) {
936
+ pat_util:: pat_bindings ( cx. tcx . def_map , * pat, |_, _, _, _| any_bindings = true )
937
+ }
938
+ if !any_bindings {
939
+ cx. span_lint ( irrefutable_match_without_binding, sp,
940
+ "irrefutable let or match without any bindings (equivalent to inner expression)" )
941
+ }
942
+ }
943
+
924
944
fn check_item_non_camel_case_types ( cx : & Context , it : & ast:: item ) {
925
945
fn is_camel_case ( cx : ty:: ctxt , ident : ast:: Ident ) -> bool {
926
946
let ident = cx. sess . str_of ( ident) ;
@@ -1319,6 +1339,9 @@ impl<'a> Visitor<()> for Context<'a> {
1319
1339
ast:: ExprParen ( expr) => if self . negated_expr_id == e. id {
1320
1340
self . negated_expr_id = expr. id
1321
1341
} ,
1342
+ ast:: ExprMatch ( _, [ ast:: Arm { ref pats, .. } ] ) => {
1343
+ check_irrefutable_match_without_binding ( self , e. span , * pats)
1344
+ } ,
1322
1345
_ => ( )
1323
1346
} ;
1324
1347
@@ -1334,6 +1357,11 @@ impl<'a> Visitor<()> for Context<'a> {
1334
1357
visit:: walk_expr ( self , e, ( ) ) ;
1335
1358
}
1336
1359
1360
+ fn visit_local ( & mut self , l : @ast:: Local , _: ( ) ) {
1361
+ check_irrefutable_match_without_binding ( self , l. span , & [ l. pat ] ) ;
1362
+ visit:: walk_local ( self , l, ( ) )
1363
+ }
1364
+
1337
1365
fn visit_stmt ( & mut self , s : @ast:: Stmt , _: ( ) ) {
1338
1366
check_path_statement ( self , s) ;
1339
1367
0 commit comments