-
Notifications
You must be signed in to change notification settings - Fork 13k
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
cyrgani
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
cyrgani:noop-method-call-clone-derive
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+156
−15
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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(); | ||
| ^^^^^^^^ | ||
|
@@ -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(); | ||
| ^^^^^^^^ | ||
|
@@ -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(); | ||
| ^^^^^^^^^ | ||
|
@@ -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>` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be ideal if we didn't repeat the type when |
||
| | ||
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(); | ||
| ^^^^^^^^ | ||
|
@@ -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(); | ||
| ^^^^^^^^ | ||
|
@@ -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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theClone
implementation for&T
incore
. How can I resolve the definition ofT::clone
(whereT
isorig_ty
) instead?There was a problem hiding this comment.
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 newty::Instance::try_resolve(cx.tcx, cx.typing_env(), did, args)
call.