mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-27 07:03:45 +00:00
Auto merge of #8462 - ken-matsui:use-precise-namespace-for-reverse, r=llogiq
Use the precise namespace for `Reverse` Closes: https://github.com/rust-lang/rust-clippy/issues/8461 changelog: [`unnecessary_sort_by`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by): Use the precise namespace for `Reverse`
This commit is contained in:
commit
d1ca1c1d0c
@ -239,7 +239,7 @@ impl LateLintPass<'_> for UnnecessarySortBy {
|
||||
if trigger.unstable { "_unstable" } else { "" },
|
||||
trigger.closure_arg,
|
||||
if trigger.reverse {
|
||||
format!("Reverse({})", trigger.closure_body)
|
||||
format!("std::cmp::Reverse({})", trigger.closure_body)
|
||||
} else {
|
||||
trigger.closure_body.to_string()
|
||||
},
|
||||
|
@ -3,7 +3,6 @@
|
||||
#![allow(clippy::stable_sort_primitive)]
|
||||
|
||||
use std::cell::Ref;
|
||||
use std::cmp::Reverse;
|
||||
|
||||
fn unnecessary_sort_by() {
|
||||
fn id(x: isize) -> isize {
|
||||
@ -18,8 +17,8 @@ fn unnecessary_sort_by() {
|
||||
vec.sort_unstable_by_key(|a| id(-a));
|
||||
// Reverse examples
|
||||
vec.sort_by(|a, b| b.cmp(a)); // not linted to avoid suggesting `Reverse(b)` which would borrow
|
||||
vec.sort_by_key(|b| Reverse((b + 5).abs()));
|
||||
vec.sort_unstable_by_key(|b| Reverse(id(-b)));
|
||||
vec.sort_by_key(|b| std::cmp::Reverse((b + 5).abs()));
|
||||
vec.sort_unstable_by_key(|b| std::cmp::Reverse(id(-b)));
|
||||
// Negative examples (shouldn't be changed)
|
||||
let c = &7;
|
||||
vec.sort_by(|a, b| (b - a).cmp(&(a - b)));
|
||||
@ -76,7 +75,6 @@ mod issue_5754 {
|
||||
|
||||
// The closure parameter is not dereferenced anymore, so non-Copy types can be linted
|
||||
mod issue_6001 {
|
||||
use super::*;
|
||||
struct Test(String);
|
||||
|
||||
impl Test {
|
||||
@ -93,8 +91,8 @@ mod issue_6001 {
|
||||
args.sort_by_key(|a| a.name());
|
||||
args.sort_unstable_by_key(|a| a.name());
|
||||
// Reverse
|
||||
args.sort_by_key(|b| Reverse(b.name()));
|
||||
args.sort_unstable_by_key(|b| Reverse(b.name()));
|
||||
args.sort_by_key(|b| std::cmp::Reverse(b.name()));
|
||||
args.sort_unstable_by_key(|b| std::cmp::Reverse(b.name()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
#![allow(clippy::stable_sort_primitive)]
|
||||
|
||||
use std::cell::Ref;
|
||||
use std::cmp::Reverse;
|
||||
|
||||
fn unnecessary_sort_by() {
|
||||
fn id(x: isize) -> isize {
|
||||
@ -76,7 +75,6 @@ mod issue_5754 {
|
||||
|
||||
// The closure parameter is not dereferenced anymore, so non-Copy types can be linted
|
||||
mod issue_6001 {
|
||||
use super::*;
|
||||
struct Test(String);
|
||||
|
||||
impl Test {
|
||||
|
@ -1,5 +1,5 @@
|
||||
error: use Vec::sort here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:15:5
|
||||
--> $DIR/unnecessary_sort_by.rs:14:5
|
||||
|
|
||||
LL | vec.sort_by(|a, b| a.cmp(b));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort()`
|
||||
@ -7,70 +7,70 @@ LL | vec.sort_by(|a, b| a.cmp(b));
|
||||
= note: `-D clippy::unnecessary-sort-by` implied by `-D warnings`
|
||||
|
||||
error: use Vec::sort here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:16:5
|
||||
--> $DIR/unnecessary_sort_by.rs:15:5
|
||||
|
|
||||
LL | vec.sort_unstable_by(|a, b| a.cmp(b));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
||||
|
||||
error: use Vec::sort_by_key here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:17:5
|
||||
--> $DIR/unnecessary_sort_by.rs:16:5
|
||||
|
|
||||
LL | vec.sort_by(|a, b| (a + 5).abs().cmp(&(b + 5).abs()));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort_by_key(|a| (a + 5).abs())`
|
||||
|
||||
error: use Vec::sort_by_key here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:18:5
|
||||
--> $DIR/unnecessary_sort_by.rs:17:5
|
||||
|
|
||||
LL | vec.sort_unstable_by(|a, b| id(-a).cmp(&id(-b)));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort_unstable_by_key(|a| id(-a))`
|
||||
|
||||
error: use Vec::sort_by_key here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:21:5
|
||||
--> $DIR/unnecessary_sort_by.rs:20:5
|
||||
|
|
||||
LL | vec.sort_by(|a, b| (b + 5).abs().cmp(&(a + 5).abs()));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort_by_key(|b| Reverse((b + 5).abs()))`
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort_by_key(|b| std::cmp::Reverse((b + 5).abs()))`
|
||||
|
||||
error: use Vec::sort_by_key here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:22:5
|
||||
--> $DIR/unnecessary_sort_by.rs:21:5
|
||||
|
|
||||
LL | vec.sort_unstable_by(|a, b| id(-b).cmp(&id(-a)));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort_unstable_by_key(|b| Reverse(id(-b)))`
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort_unstable_by_key(|b| std::cmp::Reverse(id(-b)))`
|
||||
|
||||
error: use Vec::sort_by_key here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:32:5
|
||||
--> $DIR/unnecessary_sort_by.rs:31:5
|
||||
|
|
||||
LL | vec.sort_by(|a, b| (***a).abs().cmp(&(***b).abs()));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort_by_key(|a| (***a).abs())`
|
||||
|
||||
error: use Vec::sort_by_key here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:33:5
|
||||
--> $DIR/unnecessary_sort_by.rs:32:5
|
||||
|
|
||||
LL | vec.sort_unstable_by(|a, b| (***a).abs().cmp(&(***b).abs()));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort_unstable_by_key(|a| (***a).abs())`
|
||||
|
||||
error: use Vec::sort_by_key here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:93:9
|
||||
--> $DIR/unnecessary_sort_by.rs:91:9
|
||||
|
|
||||
LL | args.sort_by(|a, b| a.name().cmp(&b.name()));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `args.sort_by_key(|a| a.name())`
|
||||
|
||||
error: use Vec::sort_by_key here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:94:9
|
||||
--> $DIR/unnecessary_sort_by.rs:92:9
|
||||
|
|
||||
LL | args.sort_unstable_by(|a, b| a.name().cmp(&b.name()));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `args.sort_unstable_by_key(|a| a.name())`
|
||||
|
||||
error: use Vec::sort_by_key here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:96:9
|
||||
--> $DIR/unnecessary_sort_by.rs:94:9
|
||||
|
|
||||
LL | args.sort_by(|a, b| b.name().cmp(&a.name()));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `args.sort_by_key(|b| Reverse(b.name()))`
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `args.sort_by_key(|b| std::cmp::Reverse(b.name()))`
|
||||
|
||||
error: use Vec::sort_by_key here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:97:9
|
||||
--> $DIR/unnecessary_sort_by.rs:95:9
|
||||
|
|
||||
LL | args.sort_unstable_by(|a, b| b.name().cmp(&a.name()));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `args.sort_unstable_by_key(|b| Reverse(b.name()))`
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `args.sort_unstable_by_key(|b| std::cmp::Reverse(b.name()))`
|
||||
|
||||
error: aborting due to 12 previous errors
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user