]> git.lizzy.rs Git - rust.git/commitdiff
Make the unused_mut lint smarter with respect to locals.
authorAriel Ben-Yehuda <ariel.byd@gmail.com>
Wed, 17 Jun 2015 22:02:58 +0000 (01:02 +0300)
committerAriel Ben-Yehuda <arielb1@mail.tau.ac.il>
Tue, 30 Jun 2015 21:12:12 +0000 (00:12 +0300)
Fixes #26332

src/libcore/iter.rs
src/libfmt_macros/lib.rs
src/librustc/middle/infer/error_reporting.rs
src/librustc_borrowck/borrowck/check_loans.rs
src/librustc_trans/save/dump_csv.rs
src/libsyntax/parse/lexer/mod.rs
src/test/compile-fail/lint-unused-mut-variables.rs

index 3026f91e853eee34f69833a1615d05da56be928a..4c8511eb1902c55f0e2fec2c09ad4934530f53ea 100644 (file)
@@ -2655,8 +2655,8 @@ fn step(&self, by: &$t) -> Option<$t> {
             #[allow(trivial_numeric_casts)]
             fn steps_between(start: &$t, end: &$t, by: &$t) -> Option<usize> {
                 if *by == 0 { return None; }
-                let mut diff: usize;
-                let mut by_u: usize;
+                let diff: usize;
+                let by_u: usize;
                 if *by > 0 {
                     if *start >= *end {
                         return Some(0);
index c2b28bd134d478f7c64be02c634f05067f89f069..7ca89cfd0c9cc828cee98ef95f313011d8bb7750 100644 (file)
@@ -399,7 +399,7 @@ fn word(&mut self) -> &'a str {
             }
             Some(..) | None => { return &self.input[..0]; }
         };
-        let mut end;
+        let end;
         loop {
             match self.cur.clone().next() {
                 Some((_, c)) if c.is_xid_continue() => {
index 17075c0cba6cabe0f64ba18bd6a7f5b73f5ef1d2..6d5b47d8ed980bb40a0d3eef2c3ea8f53f1222fb 100644 (file)
@@ -1854,7 +1854,7 @@ fn inc_counter(&self) {
     }
 
     fn give_lifetime(&self) -> ast::Lifetime {
-        let mut lifetime;
+        let lifetime;
         loop {
             let mut s = String::from("'");
             s.push_str(&num_to_string(self.counter.get()));
index f06dc105d9cc1389e803a82749b76a308b87d905..237add6ff860e6d92d1d24eb8ab9b282619cde45 100644 (file)
@@ -182,7 +182,7 @@ fn mutate(&mut self,
             None => { }
         }
 
-        self.check_assignment(assignment_id, assignment_span, assignee_cmt, mode);
+        self.check_assignment(assignment_id, assignment_span, assignee_cmt);
     }
 
     fn decl_without_init(&mut self, _id: ast::NodeId, _span: Span) { }
@@ -782,16 +782,9 @@ fn check_if_assigned_path_is_moved(&self,
     fn check_assignment(&self,
                         assignment_id: ast::NodeId,
                         assignment_span: Span,
-                        assignee_cmt: mc::cmt<'tcx>,
-                        mode: euv::MutateMode) {
+                        assignee_cmt: mc::cmt<'tcx>) {
         debug!("check_assignment(assignee_cmt={:?})", assignee_cmt);
 
-        // Initializations never cause borrow errors as they only
-        // affect a fresh local.
-        if mode == euv::Init {
-            return
-        }
-
         // Check that we don't invalidate any outstanding loans
         if let Some(loan_path) = opt_loan_path(&assignee_cmt) {
             let scope = region::CodeExtent::from_node_id(assignment_id);
@@ -801,17 +794,15 @@ fn check_assignment(&self,
             });
         }
 
-        // Local variables can always be assigned to, expect for reassignments
-        // of immutable variables (or assignments that invalidate loans,
-        // of course).
+        // Check for reassignments to (immutable) local variables. This
+        // needs to be done here instead of in check_loans because we
+        // depend on move data.
         if let mc::cat_local(local_id) = assignee_cmt.cat {
-            if assignee_cmt.mutbl.is_mutable() {
-                self.tcx().used_mut_nodes.borrow_mut().insert(local_id);
-            }
-
             let lp = opt_loan_path(&assignee_cmt).unwrap();
             self.move_data.each_assignment_of(assignment_id, &lp, |assign| {
-                if !assignee_cmt.mutbl.is_mutable() {
+                if assignee_cmt.mutbl.is_mutable() {
+                    self.tcx().used_mut_nodes.borrow_mut().insert(local_id);
+                } else {
                     self.bccx.report_reassigned_immutable_variable(
                         assignment_span,
                         &*lp,
index d86242f39cea7763e94e9d3ebff435a0402059f4..f747f2deec415a28fd8390aaa5ebf521218d4e29 100644 (file)
@@ -308,7 +308,7 @@ fn process_method(&mut self, sig: &ast::MethodSig,
 
         debug!("process_method: {}:{}", id, token::get_name(name));
 
-        let mut scope_id;
+        let scope_id;
         // The qualname for a method is the trait name or name of the struct in an impl in
         // which the method is declared in, followed by the method's name.
         let qualname = match self.tcx.impl_of_method(ast_util::local_def(id)) {
index b6b5ac5c01eee1f48f591f67bb1dc7aa0128b382..507bd9de2a11a769488fd3ade4775b15a4bfd7db 100644 (file)
@@ -598,7 +598,7 @@ fn scan_digits(&mut self, real_radix: u32, scan_radix: u32) -> usize {
 
     /// Lex a LIT_INTEGER or a LIT_FLOAT
     fn scan_number(&mut self, c: char) -> token::Lit {
-        let mut num_digits;
+        let num_digits;
         let mut base = 10;
         let start_bpos = self.last_pos;
 
index dcc82b8920ff5b51245b6fb52880011da181e6fb..8165dd0fa29c0ee7049522f2f46969a46d78bf57 100644 (file)
@@ -23,6 +23,15 @@ fn main() {
     let mut b = 3; //~ ERROR: variable does not need to be mutable
     let mut a = vec!(3); //~ ERROR: variable does not need to be mutable
     let (mut a, b) = (1, 2); //~ ERROR: variable does not need to be mutable
+    let mut a; //~ ERROR: variable does not need to be mutable
+    a = 3;
+
+    let mut b; //~ ERROR: variable does not need to be mutable
+    if true {
+        b = 3;
+    } else {
+        b = 4;
+    }
 
     match 30 {
         mut x => {} //~ ERROR: variable does not need to be mutable