@@ -883,20 +883,23 @@ fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
883
883
884
884
fn check_unused_mut_pat ( cx : & Context , p : @ast:: Pat ) {
885
885
match p. node {
886
- ast:: PatIdent ( ast:: BindByValue ( ast:: MutMutable ) , _, _) => {
887
- let mut used = false ;
888
- let mut bindings = 0 ;
889
- do pat_util:: pat_bindings ( cx. tcx . def_map , p) |_, id, _, _| {
890
- used = used || cx. tcx . used_mut_nodes . contains ( & id) ;
891
- bindings += 1 ;
892
- }
893
- if !used {
894
- let msg = if bindings == 1 {
895
- "variable does not need to be mutable"
896
- } else {
897
- "variables do not need to be mutable"
898
- } ;
899
- cx. span_lint ( unused_mut, p. span , msg) ;
886
+ ast:: PatIdent ( ast:: BindByValue ( ast:: MutMutable ) ,
887
+ ref path, _) if pat_util:: pat_is_binding ( cx. tcx . def_map , p) => {
888
+ // `let mut _a = 1;` doesn't need a warning.
889
+ let initial_underscore = match path. segments {
890
+ [ ast:: PathSegment { identifier : id, _ } ] => {
891
+ cx. tcx . sess . str_of ( id) . starts_with ( "_" )
892
+ }
893
+ _ => {
894
+ cx. tcx . sess . span_bug ( p. span ,
895
+ "mutable binding that doesn't \
896
+ consist of exactly one segment") ;
897
+ }
898
+ } ;
899
+
900
+ if !initial_underscore && !cx. tcx . used_mut_nodes . contains ( & p. id ) {
901
+ cx. span_lint ( unused_mut, p. span ,
902
+ "variable does not need to be mutable" ) ;
900
903
}
901
904
}
902
905
_ => ( )
0 commit comments