Auto merge of #111916 - fee1-dead-contrib:noop-method-call-warn, r=compiler-errors

make `noop_method_call` warn by default

r? `@compiler-errors`
This commit is contained in:
bors 2023-07-29 01:40:50 +00:00
commit 4734ac0943
17 changed files with 165 additions and 108 deletions

View file

@ -1231,8 +1231,9 @@ pub enum NonUpperCaseGlobalSub {
#[note]
pub struct NoopMethodCallDiag<'a> {
pub method: Symbol,
pub receiver_ty: Ty<'a>,
#[label]
pub orig_ty: Ty<'a>,
pub trait_: Symbol,
#[suggestion(code = "", applicability = "machine-applicable")]
pub label: Span,
}

View file

@ -18,7 +18,6 @@ declare_lint! {
///
/// ```rust
/// # #![allow(unused)]
/// #![warn(noop_method_call)]
/// struct Foo;
/// let foo = &Foo;
/// let clone: &Foo = foo.clone();
@ -34,7 +33,7 @@ declare_lint! {
/// calling `clone` on a `&T` where `T` does not implement clone, actually doesn't do anything
/// as references are copy. This lint detects these calls and warns the user about them.
pub NOOP_METHOD_CALL,
Allow,
Warn,
"detects the use of well-known noop methods"
}
@ -86,10 +85,9 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
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)
) {
let Some(trait_) = cx.tcx.get_diagnostic_name(trait_id) else { return };
if !matches!(trait_, sym::Borrow | sym::Clone | sym::Deref) {
return;
};
@ -114,11 +112,13 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
let expr_span = expr.span;
let span = expr_span.with_lo(receiver.span.hi());
let orig_ty = expr_ty.peel_refs();
if receiver_ty == expr_ty {
cx.emit_spanned_lint(
NOOP_METHOD_CALL,
span,
NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span },
NoopMethodCallDiag { method: call.ident.name, orig_ty, trait_, label: span },
);
} else {
match name {