Add test on unnecessary_sort_by when argument does not implement Ord

This commit is contained in:
dswij 2021-10-15 18:06:02 +08:00
parent ec5071931e
commit e4ac4c2e1a
3 changed files with 22 additions and 12 deletions

View File

@ -2,6 +2,7 @@
#![allow(clippy::stable_sort_primitive)]
use std::cell::Ref;
use std::cmp::Reverse;
fn unnecessary_sort_by() {
@ -33,6 +34,10 @@ fn unnecessary_sort_by() {
// `Reverse(b)` would borrow in the following cases, don't lint
vec.sort_by(|a, b| b.cmp(a));
vec.sort_unstable_by(|a, b| b.cmp(a));
// No warning if element does not implement `Ord`
let mut vec: Vec<Ref<usize>> = Vec::new();
vec.sort_unstable_by(|a, b| a.cmp(b));
}
// Do not suggest returning a reference to the closure parameter of `Vec::sort_by_key`

View File

@ -2,6 +2,7 @@
#![allow(clippy::stable_sort_primitive)]
use std::cell::Ref;
use std::cmp::Reverse;
fn unnecessary_sort_by() {
@ -33,6 +34,10 @@ fn id(x: isize) -> isize {
// `Reverse(b)` would borrow in the following cases, don't lint
vec.sort_by(|a, b| b.cmp(a));
vec.sort_unstable_by(|a, b| b.cmp(a));
// No warning if element does not implement `Ord`
let mut vec: Vec<Ref<usize>> = Vec::new();
vec.sort_unstable_by(|a, b| a.cmp(b));
}
// Do not suggest returning a reference to the closure parameter of `Vec::sort_by_key`

View File

@ -1,5 +1,5 @@
error: use Vec::sort here instead
--> $DIR/unnecessary_sort_by.rs:14:5
--> $DIR/unnecessary_sort_by.rs:15:5
|
LL | vec.sort_by(|a, b| a.cmp(b));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort()`
@ -7,67 +7,67 @@ 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:15:5
--> $DIR/unnecessary_sort_by.rs:16: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:16:5
--> $DIR/unnecessary_sort_by.rs:17: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:17:5
--> $DIR/unnecessary_sort_by.rs:18: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:20:5
--> $DIR/unnecessary_sort_by.rs:21: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()))`
error: use Vec::sort_by_key here instead
--> $DIR/unnecessary_sort_by.rs:21:5
--> $DIR/unnecessary_sort_by.rs:22:5
|
LL | vec.sort_unstable_by(|a, b| id(-b).cmp(&id(-a)));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort_unstable_by_key(|b| Reverse(id(-b)))`
error: use Vec::sort_by_key here instead
--> $DIR/unnecessary_sort_by.rs:31:5
--> $DIR/unnecessary_sort_by.rs:32: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:32:5
--> $DIR/unnecessary_sort_by.rs:33: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:88:9
--> $DIR/unnecessary_sort_by.rs:93: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:89:9
--> $DIR/unnecessary_sort_by.rs:94: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:91:9
--> $DIR/unnecessary_sort_by.rs:96:9
|
LL | args.sort_by(|a, b| b.name().cmp(&a.name()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `args.sort_by_key(|b| Reverse(b.name()))`
error: use Vec::sort_by_key here instead
--> $DIR/unnecessary_sort_by.rs:92:9
--> $DIR/unnecessary_sort_by.rs:97:9
|
LL | args.sort_unstable_by(|a, b| b.name().cmp(&a.name()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `args.sort_unstable_by_key(|b| Reverse(b.name()))`