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

noop_method_call: fix and improve derive suggestions #134903

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ lint_non_upper_case_global = {$sort} `{$name}` should have an upper case name
lint_noop_method_call = call to `.{$method}()` on a reference in this situation does nothing
.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
.derive_suggestion = if you meant to clone `{$orig_ty}`, implement `Clone` for it
.derive_suggestion = if you meant to clone `{$orig_ty}`, implement `Clone` for `{$non_clone_ty}`

lint_only_cast_u8_to_char = only `u8` can be cast into `char`
.suggestion = use a `char` literal instead
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,7 @@ pub(crate) struct NoopMethodCallDiag<'a> {
pub method: Symbol,
pub orig_ty: Ty<'a>,
pub trait_: Symbol,
pub non_clone_ty: Ty<'a>,
#[suggestion(code = "", applicability = "machine-applicable")]
pub label: Span,
#[suggestion(
Expand Down
37 changes: 36 additions & 1 deletion compiler/rustc_lint/src/noop_method_call.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_hir::def::DefKind;
use rustc_hir::{Expr, ExprKind};
use rustc_middle::ty;
use rustc_middle::ty::ClauseKind;
use rustc_middle::ty::adjustment::Adjust;
use rustc_session::{declare_lint, declare_lint_pass};
use rustc_span::sym;
Expand Down Expand Up @@ -124,14 +125,48 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
let orig_ty = expr_ty.peel_refs();

if receiver_ty == expr_ty {
let mut non_clone_ty = orig_ty;

let suggest_derive = match orig_ty.kind() {
ty::Adt(def, _) => Some(cx.tcx.def_span(def.did()).shrink_to_lo()),
ty::Adt(def, args) => {
if def.did().is_local() {
Some(cx.tcx.def_span(def.did()).shrink_to_lo())
} else if let Some(trait_impl_id) = cx.tcx.impl_of_method(i.def_id()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i.def_id() is the wrong DefId, because it points to the Clone implementation for &T in core. How can I resolve the definition of T::clone (where T is orig_ty) instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Look at https://github.com/rust-lang/rust/pull/134903/files#diff-efe45c248e022b7a8ad064214427dad6129407be362155867ef3411ce31a1486R96-R100

I think you can use let args = tcx.mk_args(&[ty::GenericArg::from(orig_ty)]); for a new ty::Instance::try_resolve(cx.tcx, cx.typing_env(), did, args) call.

// If the type is generic over `T`, implements `Clone` if `T` does
// and the concrete `T` is local, suggest deriving `Clone` for `T` rather than the type.
let predicates = cx.tcx.predicates_of(trait_impl_id);

if predicates.predicates.into_iter().all(|&(predicate, _)| {
let ClauseKind::Trait(trait_predicate) = predicate.kind().skip_binder()
else {
return true;
};
cx.tcx.is_diagnostic_item(sym::Clone, trait_predicate.trait_ref.def_id)
}) {
args.iter().find_map(|arg| {
let ty = arg.as_type()?;
let did = ty.ty_adt_def()?.did();
if did.is_local() {
non_clone_ty = ty;
Some(cx.tcx.def_span(did))
} else {
None
}
})
} else {
None
}
} else {
None
}
}
_ => None,
};
cx.emit_span_lint(NOOP_METHOD_CALL, span, NoopMethodCallDiag {
method: call.ident.name,
orig_ty,
trait_,
non_clone_ty,
label: span,
suggest_derive,
});
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/lint/auxiliary/non_clone_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub struct NotClone;

pub struct IsClone;

impl Clone for IsClone {
fn clone(&self) -> Self {
Self
}
}

pub struct ConditionalClone<T>(T);

impl<T: Clone> Clone for ConditionalClone<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

pub struct DifferentlyConditionalClone<T>(T);

impl<T: Default> Clone for DifferentlyConditionalClone<T> {
fn clone(&self) -> Self {
Self(T::default())
}
}
28 changes: 28 additions & 0 deletions tests/ui/lint/noop-method-call.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//@ check-pass
//@ aux-build:non_clone_types.rs

#![feature(rustc_attrs)]
#![allow(unused)]

extern crate non_clone_types;
use non_clone_types::*;

use std::borrow::Borrow;
use std::ops::Deref;

Expand Down Expand Up @@ -61,3 +65,27 @@ impl Clone for DiagnosticClone {
fn with_other_diagnostic_item(x: DiagnosticClone) {
x.clone();
}

fn with_foreign_type(v: &NotClone) {
v.clone();
//~^ WARN call to `.clone()` on a reference in this situation does nothing
}

fn with_foreign_generic_type(v: &ConditionalClone<PlainType<u32>>) {
v.clone();
//~^ WARN call to `.clone()` on a reference in this situation does nothing
}

fn with_only_foreign_types_1(v: &ConditionalClone<NotClone>) {
v.clone();
//~^ WARN call to `.clone()` on a reference in this situation does nothing
}

fn with_only_foreign_types_2(v: &ConditionalClone<IsClone>) {
v.clone();
}

fn different_impl_bound(v: &DifferentlyConditionalClone<PlainType<u8>>) {
v.clone();
//~^ WARN call to `.clone()` on a reference in this situation does nothing
}
78 changes: 65 additions & 13 deletions tests/ui/lint/noop-method-call.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:15:25
--> $DIR/noop-method-call.rs:19:25
|
LL | let _ = &mut encoded.clone();
| ^^^^^^^^ help: remove this redundant call
Expand All @@ -8,15 +8,15 @@ LL | let _ = &mut encoded.clone();
= note: `#[warn(noop_method_call)]` on by default

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:17:21
--> $DIR/noop-method-call.rs:21:21
|
LL | let _ = &encoded.clone();
| ^^^^^^^^ help: remove this redundant call
|
= note: the type `[u8]` does not implement `Clone`, so calling `clone` on `&[u8]` copies the reference, which does not do anything and can be removed

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:23:71
--> $DIR/noop-method-call.rs:27:71
|
LL | let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
| ^^^^^^^^
Expand All @@ -27,14 +27,14 @@ help: remove this redundant call
LL - let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
LL + let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref;
|
help: if you meant to clone `PlainType<u32>`, implement `Clone` for it
help: if you meant to clone `PlainType<u32>`, implement `Clone` for `PlainType<u32>`
|
LL + #[derive(Clone)]
LL | struct PlainType<T>(T);
|

warning: call to `.deref()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:31:63
--> $DIR/noop-method-call.rs:35:63
|
LL | let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
| ^^^^^^^^
Expand All @@ -45,14 +45,14 @@ help: remove this redundant call
LL - let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
LL + let non_deref_type_deref: &PlainType<u32> = non_deref_type;
|
help: if you meant to clone `PlainType<u32>`, implement `Clone` for it
help: if you meant to clone `PlainType<u32>`, implement `Clone` for `PlainType<u32>`
|
LL + #[derive(Clone)]
LL | struct PlainType<T>(T);
|

warning: call to `.borrow()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:35:66
--> $DIR/noop-method-call.rs:39:66
|
LL | let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
| ^^^^^^^^^
Expand All @@ -63,14 +63,14 @@ help: remove this redundant call
LL - let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
LL + let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type;
|
help: if you meant to clone `PlainType<u32>`, implement `Clone` for it
help: if you meant to clone `PlainType<u32>`, implement `Clone` for `PlainType<u32>`
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be ideal if we didn't repeat the type when orig_ty == non_clone_ty.

|
LL + #[derive(Clone)]
LL | struct PlainType<T>(T);
|

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:44:19
--> $DIR/noop-method-call.rs:48:19
|
LL | non_clone_type.clone();
| ^^^^^^^^
Expand All @@ -81,14 +81,14 @@ help: remove this redundant call
LL - non_clone_type.clone();
LL + non_clone_type;
|
help: if you meant to clone `PlainType<T>`, implement `Clone` for it
help: if you meant to clone `PlainType<T>`, implement `Clone` for `PlainType<T>`
|
LL + #[derive(Clone)]
LL | struct PlainType<T>(T);
|

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:49:19
--> $DIR/noop-method-call.rs:53:19
|
LL | non_clone_type.clone();
| ^^^^^^^^
Expand All @@ -99,11 +99,63 @@ help: remove this redundant call
LL - non_clone_type.clone();
LL + non_clone_type;
|
help: if you meant to clone `PlainType<u32>`, implement `Clone` for it
help: if you meant to clone `PlainType<u32>`, implement `Clone` for `PlainType<u32>`
|
LL + #[derive(Clone)]
LL | struct PlainType<T>(T);
|

warning: 7 warnings emitted
warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:70:6
|
LL | v.clone();
| ^^^^^^^^ help: remove this redundant call
|
= note: the type `non_clone_types::NotClone` does not implement `Clone`, so calling `clone` on `&non_clone_types::NotClone` copies the reference, which does not do anything and can be removed

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:75:6
|
LL | v.clone();
| ^^^^^^^^
|
= note: the type `non_clone_types::ConditionalClone<PlainType<u32>>` does not implement `Clone`, so calling `clone` on `&non_clone_types::ConditionalClone<PlainType<u32>>` copies the reference, which does not do anything and can be removed
help: remove this redundant call
|
LL - v.clone();
LL + v;
|
help: if you meant to clone `non_clone_types::ConditionalClone<PlainType<u32>>`, implement `Clone` for `PlainType<u32>`
|
LL + #[derive(Clone)]
LL | struct PlainType<T>(T);
|

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:80:6
|
LL | v.clone();
| ^^^^^^^^ help: remove this redundant call
|
= note: the type `non_clone_types::ConditionalClone<non_clone_types::NotClone>` does not implement `Clone`, so calling `clone` on `&non_clone_types::ConditionalClone<non_clone_types::NotClone>` copies the reference, which does not do anything and can be removed

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:89:6
|
LL | v.clone();
| ^^^^^^^^
|
= note: the type `non_clone_types::DifferentlyConditionalClone<PlainType<u8>>` does not implement `Clone`, so calling `clone` on `&non_clone_types::DifferentlyConditionalClone<PlainType<u8>>` copies the reference, which does not do anything and can be removed
help: remove this redundant call
|
LL - v.clone();
LL + v;
|
help: if you meant to clone `non_clone_types::DifferentlyConditionalClone<PlainType<u8>>`, implement `Clone` for `PlainType<u8>`
|
LL + #[derive(Clone)]
LL | struct PlainType<T>(T);
|

warning: 11 warnings emitted

Loading