-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Restrict unnecessary_sort_by to non-reference, Copy types #6006
Merged
Conversation
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
r? @Manishearth (rust_highfive has picked a reviewer for you, use r? to override) |
rust-highfive
added
the
S-waiting-on-review
Status: Awaiting review from the assignee but also interested parties
label
Sep 4, 2020
@bors r+ |
📌 Commit 9950092 has been approved by |
bors
added a commit
that referenced
this pull request
Sep 6, 2020
Restrict unnecessary_sort_by to non-reference, Copy types `Vec::sort_by_key` closure parameter is `F: FnMut(&T) -> K`. The lint's suggestion destructures the `T` parameter; this was probably done to avoid different unnamed lifetimes when `K = Reverse<&T>`. This change fixes two issues: * Destructuring T when T is non-reference requires the type to be Copy, otherwise we would try to move from a shared reference. We make sure `T: Copy` holds. * Make sure `T` is actually non-reference. I didn't go for destructuring multiple levels of references, as we would have to compensate in the closure body by removing derefs and maybe adding parens, which would add more complexity. changelog: Restrict [`unnecessary_sort_by`] to non-reference, Copy types Fixes #6001
💔 Test failed - checks-action_test |
ebroto
force-pushed
the
6001_unnecessary_sort_by
branch
from
September 6, 2020 22:13
9950092
to
b22f50e
Compare
ebroto
force-pushed
the
6001_unnecessary_sort_by
branch
from
September 6, 2020 22:19
b22f50e
to
9fa9208
Compare
@bors r=Manishearth |
📌 Commit 9fa9208 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
bors
added a commit
that referenced
this pull request
Oct 6, 2020
unnecessary sort by: avoid dereferencing the suggested closure parameter This change tries to simplify the solution for problematic cases but is less restrictive than #6006. * We can't dereference shared references to non-Copy types, so the new suggestion does not do that. Note that this implies that the suggested closure parameter will be a reference. * We can't take a reference to the closure parameter in the returned key, so we don't lint in those cases. This can happen either because the key borrows from the parameter (e.g. `|a| a.borrows()`), or because we suggest `|a| Reverse(a)`. If we did we would hit this error: ``` error: lifetime may not live long enough --> /home/ebroto/src/ebroto-clippy/tests/ui/unnecessary_sort_by.fixed:19:25 | 19 | vec.sort_by_key(|b| Reverse(b)); | -- ^^^^^^^^^^ returning this value requires that `'1` must outlive `'2` | || | |return type of closure is Reverse<&'2 isize> | has type `&'1 isize` error: aborting due to previous error ``` Note that Clippy does not currently have the (MIR-based) machinery necessary to check that what is borrowed is actually the closure parameter. changelog: [`unnecessary_sort_by`]: avoid dereferencing the suggested closure parameter Fixes #6001
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Vec::sort_by_key
closure parameter isF: FnMut(&T) -> K
. The lint's suggestion destructures theT
parameter; this was probably done to avoid different unnamed lifetimes whenK = Reverse<&T>
.This change fixes two issues:
T: Copy
holds.T
is actually non-reference. I didn't go for destructuring multiple levels of references, as we would have to compensate in the closure body by removing derefs and maybe adding parens, which would add more complexity.changelog: Restrict [
unnecessary_sort_by
] to non-reference, Copy typesFixes #6001