-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Suggest using clone when we have &T and T implemented Clone #106497
Merged
bors
merged 5 commits into
rust-lang:master
from
chenyukang:yukang/fix-106443-sugg-clone
Jan 9, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e7a7778
Suggest possible clone when we have &T
chenyukang 1a0a613
Apply suggestions from code review
chenyukang ce4afed
comments feedback
chenyukang 6082729
use type_implements_trait to check Param clone
chenyukang 0e570e5
Remove extra space
chenyukang 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
23 changes: 23 additions & 0 deletions
23
src/test/ui/suggestions/issue-106443-sugg-clone-for-arg.rs
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,23 @@ | ||
#[derive(Clone)] | ||
struct S; | ||
|
||
// without Clone | ||
struct T; | ||
|
||
fn foo(_: S) {} | ||
|
||
fn test1() { | ||
let s = &S; | ||
foo(s); //~ ERROR mismatched types | ||
} | ||
|
||
fn bar(_: T) {} | ||
fn test2() { | ||
let t = &T; | ||
bar(t); //~ ERROR mismatched types | ||
} | ||
|
||
fn main() { | ||
test1(); | ||
test2(); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/ui/suggestions/issue-106443-sugg-clone-for-arg.stderr
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,35 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-106443-sugg-clone-for-arg.rs:11:9 | ||
| | ||
LL | foo(s); | ||
| --- ^ expected struct `S`, found `&S` | ||
| | | ||
| arguments to this function are incorrect | ||
| | ||
note: function defined here | ||
--> $DIR/issue-106443-sugg-clone-for-arg.rs:7:4 | ||
| | ||
LL | fn foo(_: S) {} | ||
| ^^^ ---- | ||
help: consider using clone here | ||
| | ||
LL | foo(s.clone()); | ||
| ++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-106443-sugg-clone-for-arg.rs:17:9 | ||
| | ||
LL | bar(t); | ||
| --- ^ expected struct `T`, found `&T` | ||
| | | ||
| arguments to this function are incorrect | ||
| | ||
note: function defined here | ||
--> $DIR/issue-106443-sugg-clone-for-arg.rs:14:4 | ||
| | ||
LL | fn bar(_: T) {} | ||
| ^^^ ---- | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
20 changes: 20 additions & 0 deletions
20
src/test/ui/suggestions/issue-106443-sugg-clone-for-bound.rs
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,20 @@ | ||
#[derive(Clone)] | ||
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. This test can be marked as |
||
struct S; | ||
|
||
trait X {} | ||
|
||
impl X for S {} | ||
|
||
fn foo<T: X>(_: T) {} | ||
fn bar<T: X>(s: &T) { | ||
foo(s); //~ ERROR the trait bound `&T: X` is not satisfied | ||
} | ||
|
||
fn bar_with_clone<T: X + Clone>(s: &T) { | ||
foo(s); //~ ERROR the trait bound `&T: X` is not satisfied | ||
} | ||
|
||
fn main() { | ||
let s = &S; | ||
bar(s); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/ui/suggestions/issue-106443-sugg-clone-for-bound.stderr
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,29 @@ | ||
error[E0277]: the trait bound `&T: X` is not satisfied | ||
--> $DIR/issue-106443-sugg-clone-for-bound.rs:10:9 | ||
| | ||
LL | foo(s); | ||
| ^ the trait `X` is not implemented for `&T` | ||
| | ||
help: consider further restricting this bound | ||
| | ||
LL | fn bar<T: X + Clone>(s: &T) { | ||
| +++++++ | ||
help: consider using clone here | ||
| | ||
LL | foo(s.clone()); | ||
| ++++++++ | ||
|
||
error[E0277]: the trait bound `&T: X` is not satisfied | ||
--> $DIR/issue-106443-sugg-clone-for-bound.rs:14:9 | ||
| | ||
LL | foo(s); | ||
| ^ the trait `X` is not implemented for `&T` | ||
| | ||
help: consider using clone here | ||
| | ||
LL | foo(s.clone()); | ||
| ++++++++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
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.
Looking at this I'm thinking we should also check if the type implements
Drop
, and if so tweak the wording to be less confident, but this can be addressed in a later PR.