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:
commit
4734ac0943
17 changed files with 165 additions and 108 deletions
|
@ -410,8 +410,8 @@ lint_non_upper_case_global = {$sort} `{$name}` should have an upper case name
|
|||
.label = should have an UPPER_CASE name
|
||||
|
||||
lint_noop_method_call = call to `.{$method}()` on a reference in this situation does nothing
|
||||
.label = unnecessary method call
|
||||
.note = the type `{$receiver_ty}` which `{$method}` is being called on is the same as the type returned from `{$method}`, so the method call does not do anything and can be removed
|
||||
.suggestion = remove this redundant call
|
||||
.note = the type `{$orig_ty}` does not implement `{$trait_}`, so calling `{$method}` on `&{$orig_ty}` copies the reference, which does not do anything and can be removed
|
||||
|
||||
lint_only_cast_u8_to_char = only `u8` can be cast into `char`
|
||||
.suggestion = use a `char` literal instead
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue