]> git.lizzy.rs Git - rust.git/commitdiff
add lint for mutable borrow; may have false positives. pushed for feedback
authorLaura Peskin <laura.peskin@gmail.com>
Mon, 25 Sep 2017 06:00:21 +0000 (02:00 -0400)
committerLaura Peskin <laura.peskin@gmail.com>
Mon, 25 Sep 2017 06:00:21 +0000 (02:00 -0400)
clippy_lints/src/loops.rs
tests/run-pass/mut_range_bound_tmp.rs

index 518b6e2134782a444a4a66446eea05095ea64206..c82ddb7383d312ec0ec83f93c162303d4aa2ab67 100644 (file)
@@ -1327,7 +1327,20 @@ fn matched_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: MatchMode) {
     fn consume_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: ConsumeMode) {
     }
 
-    fn borrow(&mut self, _: NodeId, _: Span, _: cmt<'tcx>, _: ty::Region, _: ty::BorrowKind, _: LoanCause) {        
+    fn borrow(&mut self, _: NodeId, sp: Span, cmt: cmt<'tcx>, _: ty::Region, bk: ty::BorrowKind, _: LoanCause) {
+        match bk {
+            ty::BorrowKind::MutBorrow => {
+                if let Categorization::Local(id) = cmt.cat {
+                    if Some(id) == self.node_id_low {
+                        self.span_low = Some(sp)
+                    }
+                    if Some(id) == self.node_id_high {
+                        self.span_high = Some(sp)
+                    }
+                }
+            },
+            _ => (),
+        }
     }
 
     fn mutate(&mut self, _: NodeId, sp: Span, cmt: cmt<'tcx>, _: MutateMode) {
@@ -1390,8 +1403,8 @@ fn check_for_mutability(cx: &LateContext, bound: &Expr) -> Option<NodeId> {
 
 fn check_for_mutation(cx: &LateContext, body: &Expr, bound_ids: Vec<Option<NodeId>>) -> (Option<Span>, Option<Span>) {
     let mut delegate = MutateDelegate { node_id_low: bound_ids[0], node_id_high: bound_ids[1], span_low: None, span_high: None };
-    let d = def_id::DefId::local(body.hir_id.owner);
-    let region_scope_tree = &cx.tcx.region_scope_tree(d);
+    let def_id = def_id::DefId::local(body.hir_id.owner);
+    let region_scope_tree = &cx.tcx.region_scope_tree(def_id);
     ExprUseVisitor::new(&mut delegate, cx.tcx, cx.param_env, region_scope_tree, cx.tables).walk_expr(body);
     return delegate.mutation_span();
 }
index c13c0c0ae8538b9092932256468a9497bc175bae..1a7159c330e3bdfb21ec6914c77a40974d66db75 100644 (file)
@@ -9,6 +9,8 @@ fn main() {
     mut_range_bound_both();
     mut_range_bound_no_mutation();
     immut_range_bound();
+    mut_borrow_range_bound();
+    immut_borrow_range_bound();
 }
 
 fn mut_range_bound_upper() {
@@ -35,8 +37,15 @@ fn mut_range_bound_no_mutation() {
 fn mut_borrow_range_bound() {
     let mut m = 4;
     for i in 0..m {
-        let n = &mut m;
-        *n += 1;
+        let n = &mut m;  // warning here?
+        *n += 1;         // or here?
+    }
+}
+
+fn immut_borrow_range_bound() {
+    let mut m = 4;
+    for i in 0..m {
+        let n = &m;   // should be no warning?
     }
 }