about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTakayuki Nakata <f.seasons017@gmail.com>2020-07-22 22:11:31 +0900
committerTakayuki Nakata <f.seasons017@gmail.com>2020-08-05 23:00:03 +0900
commit228f668282daab05ec20adbbdeb227e923d10864 (patch)
tree94e80884f5e91bf6e7a56cec5fca9973a34b0aee
parentb7ceb4d3d7ed3ea7039caf803073e86ad3643e21 (diff)
downloadrust-228f668282daab05ec20adbbdeb227e923d10864.tar.gz
rust-228f668282daab05ec20adbbdeb227e923d10864.zip
Use `mutated_variables`
-rw-r--r--clippy_lints/src/loops.rs38
1 files changed, 1 insertions, 37 deletions
diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs
index 3a1fbc4bfed..86952c10dfc 100644
--- a/clippy_lints/src/loops.rs
+++ b/clippy_lints/src/loops.rs
@@ -1052,42 +1052,6 @@ fn detect_manual_memcpy<'tcx>(
     }
 }
 
-// Delegate that traverses expression and detects mutable variables being used
-struct UsesMutableDelegate {
-    found_mutable: bool,
-}
-
-impl<'tcx> Delegate<'tcx> for UsesMutableDelegate {
-    fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: ConsumeMode) {}
-
-    fn borrow(&mut self, _: &PlaceWithHirId<'tcx>, bk: ty::BorrowKind) {
-        // Mutable variable is found
-        if let ty::BorrowKind::MutBorrow = bk {
-            self.found_mutable = true;
-        }
-    }
-
-    fn mutate(&mut self, _: &PlaceWithHirId<'tcx>) {}
-}
-
-// Uses UsesMutableDelegate to find mutable variables in an expression expr
-fn has_mutable_variables<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
-    let mut delegate = UsesMutableDelegate { found_mutable: false };
-    let def_id = expr.hir_id.owner.to_def_id();
-    cx.tcx.infer_ctxt().enter(|infcx| {
-        ExprUseVisitor::new(
-            &mut delegate,
-            &infcx,
-            def_id.expect_local(),
-            cx.param_env,
-            cx.typeck_results(),
-        )
-        .walk_expr(expr);
-    });
-
-    delegate.found_mutable
-}
-
 // Scans for the usage of the for loop pattern
 struct ForPatternVisitor<'a, 'tcx> {
     found_pattern: bool,
@@ -1253,7 +1217,7 @@ fn detect_same_item_push<'tcx>(
     if same_item_push_visitor.should_lint {
         if let Some((vec, pushed_item)) = same_item_push_visitor.vec_push {
             // Make sure that the push does not involve possibly mutating values
-            if !has_mutable_variables(cx, pushed_item) {
+            if mutated_variables(pushed_item, cx).map_or(false, |mutvars| mutvars.is_empty()) {
                 // Walk through the expression being pushed and make sure that it
                 // does not contain the for loop pattern
                 let mut for_pat_visitor = ForPatternVisitor {