about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-10-29 00:56:17 +0000
committerbors <bors@rust-lang.org>2022-10-29 00:56:17 +0000
commit0ab512c568ab8366604a7dc78d44bb5828d0f5b5 (patch)
treec1de2a2e5d65c1d6577967725b7d0e36a785e4c1
parent71ddf815a04dd9241beb78697bff6aae7c2629b5 (diff)
parentc42626f96958a04871c9a662fafc71e99feba48e (diff)
downloadrust-0ab512c568ab8366604a7dc78d44bb5828d0f5b5.tar.gz
rust-0ab512c568ab8366604a7dc78d44bb5828d0f5b5.zip
Auto merge of #9711 - smoelius:issue-9710, r=Jarcho
Fix `needless_borrow` false positive #9710

Fixes #9710

changelog: fix `needless_borrow` false positive #9710
-rw-r--r--clippy_lints/src/dereference.rs3
-rw-r--r--tests/ui/needless_borrow.fixed12
-rw-r--r--tests/ui/needless_borrow.rs12
3 files changed, 27 insertions, 0 deletions
diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs
index 7e2e32a20d4..4d2f5eea105 100644
--- a/clippy_lints/src/dereference.rs
+++ b/clippy_lints/src/dereference.rs
@@ -9,6 +9,7 @@ use clippy_utils::{
 };
 use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX};
 use rustc_data_structures::fx::FxIndexMap;
+use rustc_data_structures::graph::iterate::{CycleDetector, TriColorDepthFirstSearch};
 use rustc_errors::Applicability;
 use rustc_hir::intravisit::{walk_ty, Visitor};
 use rustc_hir::{
@@ -1242,6 +1243,8 @@ fn referent_used_exactly_once<'a, 'tcx>(
         && let Some(statement) = mir.basic_blocks[location.block].statements.get(location.statement_index)
         && let StatementKind::Assign(box (_, Rvalue::Ref(_, _, place))) = statement.kind
         && !place.has_deref()
+        // Ensure not in a loop (https://github.com/rust-lang/rust-clippy/issues/9710)
+        && TriColorDepthFirstSearch::new(&mir.basic_blocks).run_from(location.block, &mut CycleDetector).is_none()
     {
         let body_owner_local_def_id = cx.tcx.hir().enclosing_body_owner(reference.hir_id);
         if possible_borrowers
diff --git a/tests/ui/needless_borrow.fixed b/tests/ui/needless_borrow.fixed
index 57a682d62c0..9931fab04eb 100644
--- a/tests/ui/needless_borrow.fixed
+++ b/tests/ui/needless_borrow.fixed
@@ -408,3 +408,15 @@ mod issue_9111 {
         a.extend(&[]); // vs a.extend([]);
     }
 }
+
+#[allow(dead_code)]
+mod issue_9710 {
+    fn main() {
+        let string = String::new();
+        for _i in 0..10 {
+            f(&string);
+        }
+    }
+
+    fn f<T: AsRef<str>>(_: T) {}
+}
diff --git a/tests/ui/needless_borrow.rs b/tests/ui/needless_borrow.rs
index 0d325b48ab8..4460f16d191 100644
--- a/tests/ui/needless_borrow.rs
+++ b/tests/ui/needless_borrow.rs
@@ -408,3 +408,15 @@ mod issue_9111 {
         a.extend(&[]); // vs a.extend([]);
     }
 }
+
+#[allow(dead_code)]
+mod issue_9710 {
+    fn main() {
+        let string = String::new();
+        for _i in 0..10 {
+            f(&string);
+        }
+    }
+
+    fn f<T: AsRef<str>>(_: T) {}
+}