Skip to content

Commit 1e36f7e

Browse files
committed
suspicious_double_ref_op: don't lint on .borrow()
1 parent 970058e commit 1e36f7e

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

compiler/rustc_lint/src/noop_method_call.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,22 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
7676

7777
// We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow`
7878
// traits and ignore any other method call.
79-
let did = match cx.typeck_results().type_dependent_def(expr.hir_id) {
80-
// Verify we are dealing with a method/associated function.
81-
Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
82-
// Check that we're dealing with a trait method for one of the traits we care about.
83-
Some(trait_id)
84-
if matches!(
85-
cx.tcx.get_diagnostic_name(trait_id),
86-
Some(sym::Borrow | sym::Clone | sym::Deref)
87-
) =>
88-
{
89-
did
90-
}
91-
_ => return,
92-
},
93-
_ => return,
79+
80+
let Some((DefKind::AssocFn, did)) =
81+
cx.typeck_results().type_dependent_def(expr.hir_id)
82+
else {
83+
return;
84+
};
85+
86+
let Some(trait_id) = cx.tcx.trait_of_item(did) else { return };
87+
88+
if !matches!(
89+
cx.tcx.get_diagnostic_name(trait_id),
90+
Some(sym::Borrow | sym::Clone | sym::Deref)
91+
) {
92+
return;
9493
};
94+
9595
let substs = cx
9696
.tcx
9797
.normalize_erasing_regions(cx.param_env, cx.typeck_results().node_substs(expr.hir_id));
@@ -129,6 +129,9 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
129129
NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span },
130130
);
131131
} else {
132+
if op == "borrow" {
133+
return;
134+
}
132135
cx.emit_spanned_lint(
133136
SUSPICIOUS_DOUBLE_REF_OP,
134137
span,

tests/ui/lint/issue-112489.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// check-pass
2+
use std::borrow::Borrow;
3+
4+
struct S;
5+
6+
trait T: Sized {
7+
fn foo(self) {}
8+
}
9+
10+
impl T for S {}
11+
impl T for &S {}
12+
13+
fn main() {
14+
let s = S;
15+
s.borrow().foo();
16+
s.foo();
17+
}

0 commit comments

Comments
 (0)