Skip to content

Commit 5711ea3

Browse files
committed
Fix #9710
1 parent df67ebb commit 5711ea3

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

clippy_lints/src/dereference.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use clippy_utils::{
99
};
1010
use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX};
1111
use rustc_data_structures::fx::FxIndexMap;
12+
use rustc_data_structures::graph::iterate::{CycleDetector, TriColorDepthFirstSearch};
1213
use rustc_errors::Applicability;
1314
use rustc_hir::intravisit::{walk_ty, Visitor};
1415
use rustc_hir::{
@@ -1201,6 +1202,8 @@ fn referent_used_exactly_once<'tcx>(
12011202
&& let Some(statement) = mir.basic_blocks[location.block].statements.get(location.statement_index)
12021203
&& let StatementKind::Assign(box (_, Rvalue::Ref(_, _, place))) = statement.kind
12031204
&& !place.has_deref()
1205+
// Ensure not in a loop (https://github.com/rust-lang/rust-clippy/issues/9710)
1206+
&& TriColorDepthFirstSearch::new(&mir.basic_blocks).run_from(location.block, &mut CycleDetector).is_none()
12041207
{
12051208
let body_owner_local_def_id = cx.tcx.hir().enclosing_body_owner(reference.hir_id);
12061209
if possible_borrowers

tests/ui/needless_borrow.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,15 @@ mod used_more_than_once {
385385
fn use_x(_: impl AsRef<str>) {}
386386
fn use_x_again(_: impl AsRef<str>) {}
387387
}
388+
389+
#[allow(dead_code)]
390+
mod issue_9710 {
391+
fn main() {
392+
let string = String::new();
393+
for _i in 0..10 {
394+
f(&string);
395+
}
396+
}
397+
398+
fn f<T: AsRef<str>>(_: T) {}
399+
}

tests/ui/needless_borrow.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,15 @@ mod used_more_than_once {
385385
fn use_x(_: impl AsRef<str>) {}
386386
fn use_x_again(_: impl AsRef<str>) {}
387387
}
388+
389+
#[allow(dead_code)]
390+
mod issue_9710 {
391+
fn main() {
392+
let string = String::new();
393+
for _i in 0..10 {
394+
f(&string);
395+
}
396+
}
397+
398+
fn f<T: AsRef<str>>(_: T) {}
399+
}

0 commit comments

Comments
 (0)