Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suspicious_double_ref_op: don't lint on .borrow() #112517

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions compiler/rustc_lint/src/noop_method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,22 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {

// We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow`
// 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.
Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
// Check that we're dealing with a trait method for one of the traits we care about.
Some(trait_id)
if matches!(
cx.tcx.get_diagnostic_name(trait_id),
Some(sym::Borrow | sym::Clone | sym::Deref)
) =>
{
did
}
_ => return,
},
_ => return,

let Some((DefKind::AssocFn, did)) =
cx.typeck_results().type_dependent_def(expr.hir_id)
else {
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
.tcx
.normalize_erasing_regions(cx.param_env, cx.typeck_results().node_substs(expr.hir_id));
Expand Down Expand Up @@ -129,6 +129,9 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span },
);
} else {
if op == "borrow" {
compiler-errors marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment here explaining why borrow makes sense?

return;
}
cx.emit_spanned_lint(
SUSPICIOUS_DOUBLE_REF_OP,
span,
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/lint/issue-112489.rs
Original file line number Diff line number Diff line change
@@ -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();
}