suspicious_double_ref_op
: don't lint on .borrow()
This commit is contained in:
parent
970058e16b
commit
1e36f7e9ae
2 changed files with 35 additions and 15 deletions
|
@ -76,22 +76,22 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
|
||||||
|
|
||||||
// We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow`
|
// We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow`
|
||||||
// traits and ignore any other method call.
|
// traits and ignore any other method call.
|
||||||
let did = match cx.typeck_results().type_dependent_def(expr.hir_id) {
|
|
||||||
// Verify we are dealing with a method/associated function.
|
let Some((DefKind::AssocFn, did)) =
|
||||||
Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
|
cx.typeck_results().type_dependent_def(expr.hir_id)
|
||||||
// Check that we're dealing with a trait method for one of the traits we care about.
|
else {
|
||||||
Some(trait_id)
|
return;
|
||||||
if matches!(
|
|
||||||
cx.tcx.get_diagnostic_name(trait_id),
|
|
||||||
Some(sym::Borrow | sym::Clone | sym::Deref)
|
|
||||||
) =>
|
|
||||||
{
|
|
||||||
did
|
|
||||||
}
|
|
||||||
_ => return,
|
|
||||||
},
|
|
||||||
_ => return,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let Some(trait_id) = cx.tcx.trait_of_item(did) else { return };
|
||||||
|
|
||||||
|
if !matches!(
|
||||||
|
cx.tcx.get_diagnostic_name(trait_id),
|
||||||
|
Some(sym::Borrow | sym::Clone | sym::Deref)
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let substs = cx
|
let substs = cx
|
||||||
.tcx
|
.tcx
|
||||||
.normalize_erasing_regions(cx.param_env, cx.typeck_results().node_substs(expr.hir_id));
|
.normalize_erasing_regions(cx.param_env, cx.typeck_results().node_substs(expr.hir_id));
|
||||||
|
@ -129,6 +129,9 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
|
||||||
NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span },
|
NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span },
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
if op == "borrow" {
|
||||||
|
return;
|
||||||
|
}
|
||||||
cx.emit_spanned_lint(
|
cx.emit_spanned_lint(
|
||||||
SUSPICIOUS_DOUBLE_REF_OP,
|
SUSPICIOUS_DOUBLE_REF_OP,
|
||||||
span,
|
span,
|
||||||
|
|
17
tests/ui/lint/issue-112489.rs
Normal file
17
tests/ui/lint/issue-112489.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
// check-pass
|
||||||
|
use std::borrow::Borrow;
|
||||||
|
|
||||||
|
struct S;
|
||||||
|
|
||||||
|
trait T: Sized {
|
||||||
|
fn foo(self) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl T for S {}
|
||||||
|
impl T for &S {}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let s = S;
|
||||||
|
s.borrow().foo();
|
||||||
|
s.foo();
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue