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

Conversation

cyrgani
Copy link
Contributor

@cyrgani cyrgani commented Dec 29, 2024

Fixes #134471.
If &T is being cloned and T is not crate-local, stop suggesting #[derive(Clone)] for T in the message.
If &T<U> is being cloned and T is not crate-local, but implements Clone if U: Clone, then try suggesting #[derive(Clone)] for U if it is local.

r? estebank

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 29, 2024
LL - v.clone();
LL + v;
|
help: if you meant to clone `non_clone_types::DifferentlyConditionalClone<PlainType<u8>>`, implement `Clone` for it
Copy link
Contributor Author

@cyrgani cyrgani Dec 29, 2024

Choose a reason for hiding this comment

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

I'm not sure why this derive below is being suggested, since the bound is T: Default instead of T: Clone here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like the problem is that cx.tcx.predicates_of(trait_impl_id) is actually empty in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Same with this, you can modify

    .derive_suggestion = if you meant to clone `{$orig_ty}`, implement `Clone` for it
to be
.derive_suggestion = if you meant to clone `{$non_clone_ty}`, implement `Clone` for it

tests/ui/lint/noop-method-call.stderr Outdated Show resolved Hide resolved
LL - v.clone();
LL + v;
|
help: if you meant to clone `non_clone_types::DifferentlyConditionalClone<PlainType<u8>>`, implement `Clone` for it
Copy link
Contributor

Choose a reason for hiding this comment

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

Same with this, you can modify

    .derive_suggestion = if you meant to clone `{$orig_ty}`, implement `Clone` for it
to be
.derive_suggestion = if you meant to clone `{$non_clone_ty}`, implement `Clone` for it

@cyrgani cyrgani force-pushed the noop-method-call-clone-derive branch from 7437998 to ad93cb8 Compare January 3, 2025 14:12
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.

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect suggestion to derive Clone on Vec directly
3 participants