-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
When suggesting borrow, remove useless clones #61143
Conversation
r? @oli-obk (rust_highfive has picked a reviewer for you, use r? to override) |
@bors r+ |
📌 Commit 8ce063a has been approved by |
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.
r=me with trim_end_matches(".clone()")
removed
@bors r- |
📌 Commit 34c4117 has been approved by |
🌲 The tree is currently closed for pull requests below priority 500, this pull request will be tested once the tree is reopened |
When suggesting borrow, remove useless clones Fix rust-lang#61106.
When suggesting borrow, remove useless clones Fix rust-lang#61106.
When suggesting borrow, remove useless clones Fix #61106.
☀️ Test successful - checks-travis, status-appveyor |
Tested on commit rust-lang/rust@9f06855. Direct link to PR: <rust-lang/rust#61143> 🎉 rls on linux: test-fail → test-pass (cc @Xanewok, @rust-lang/infra).
) { | ||
// If this expression had a clone call when suggesting borrowing | ||
// we want to suggest removing it because it'd now be unecessary. | ||
sugg_sp = arg.span; |
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 named sugg_expr
, inside the if let
below?) to arg
, so that the needs_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:
error[E0308]: mismatched types
--> src/main.rs:2:19
|
2 | let x: &i32 = (1i32 + 2).clone();
| ^^^^^^^^^^^^^^^^^^
| |
| expected &i32, found i32
| help: consider borrowing here: `&(1i32 + 2)`
|
= note: expected type `&i32`
found type `i32`
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 the clone
method call is 1i32 + 2
(which I would've assumed doesn't include parens in its own Span
).
Fix #61106.