-
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
Incorrect suggestion in "unnecessary-sort-by" #6001
Comments
I will work on this one |
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
Hm, that's not really a fix, is it? The lint was very useful here and helped improve the code. Just the auto-suggestions was wrong. |
The problem is that the lint suggests using pub struct Reverse<T>(pub T); For example in this case: // Reverse examples
vec.sort_by(|a, b| b.cmp(a)); The suggestion ends up being: vec.sort_by_key(|b| Reverse(b)); If we don't destructure the referenced type, we hit rust-lang/rust#34162
But after giving this a bit more thought I agree and this is too restrictive, we should be able to identify when the argument to I will give this another shot. |
Or maybe if the situation is too complicated (references / non- |
I ran in to failure with the suggestion applied to
In my case I'm just taking the reference and giving Thank you for working on this. This is a great clippy. |
This code triggers the following suggestion:
However, the suggested code does not work:
&a
is rejected as a pattern, it says I "cannot move out of a shared reference". using justa
for the closure argument works.The text was updated successfully, but these errors were encountered: