-
Notifications
You must be signed in to change notification settings - Fork 13.3k
When suggesting borrow, remove useless clones #61143
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bdb05a8
When suggesting to borrow, remove useless clones
estebank 8ce063a
Verify that the clone method call actually corresponds to std::clone:…
estebank ae8d6a8
review comments: avoid string modification
estebank 34c4117
review comment: do not rely on path str to identify std::clone::Clone
estebank 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 hidden or 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 hidden or 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,6 @@ | ||
fn main() { | ||
let x = String::new(); | ||
foo(x.clone()); //~ ERROR mismatched types | ||
} | ||
|
||
fn foo(_: &str) {} |
This file contains hidden or 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,15 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-61106.rs:3:9 | ||
| | ||
LL | foo(x.clone()); | ||
| ^^^^^^^^^ | ||
| | | ||
| expected &str, found struct `std::string::String` | ||
| help: consider borrowing here: `&x` | ||
| | ||
= note: expected type `&str` | ||
found type `std::string::String` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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.
You should also change
expr
(maybe better namedsugg_expr
, inside theif let
below?) toarg
, so that theneeds_parens
logic, and everything else, works correctly.(e.g. I suspect you print
&1 + 2
instead of&(1 + 2)
for(1 + 2).clone()
)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.
Nope, it works just fine:
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.
That's mildly disturbing - the code is technically wrong, since it's looking at the whole method call when determining whether it needs parens, but happens to produce the correct result.
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.
It's because the logic is to add parentheses to the code when it is not already int he code. In this case the parentheses are already there so the expression doesn't need to change.
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.
ExprKind::Paren
doesn't exist in the HIR, the first argument of theclone
method call is1i32 + 2
(which I would've assumed doesn't include parens in its ownSpan
).