Create stable_sort_primitive lint
This commit is contained in:
parent
2e0f8b6cc6
commit
25abd7ae76
11 changed files with 294 additions and 7 deletions
32
tests/ui/stable_sort_primitive.fixed
Normal file
32
tests/ui/stable_sort_primitive.fixed
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// run-rustfix
|
||||
#![warn(clippy::stable_sort_primitive)]
|
||||
|
||||
fn main() {
|
||||
// positive examples
|
||||
let mut vec = vec![1, 3, 2];
|
||||
vec.sort_unstable();
|
||||
let mut vec = vec![false, false, true];
|
||||
vec.sort_unstable();
|
||||
let mut vec = vec!['a', 'A', 'c'];
|
||||
vec.sort_unstable();
|
||||
let mut vec = vec!["ab", "cd", "ab", "bc"];
|
||||
vec.sort_unstable();
|
||||
let mut vec = vec![(2, 1), (1, 2), (2, 5)];
|
||||
vec.sort_unstable();
|
||||
let mut vec = vec![[2, 1], [1, 2], [2, 5]];
|
||||
vec.sort_unstable();
|
||||
let mut arr = [1, 3, 2];
|
||||
arr.sort_unstable();
|
||||
// Negative examples: behavior changes if made unstable
|
||||
let mut vec = vec![1, 3, 2];
|
||||
vec.sort_by_key(|i| i / 2);
|
||||
vec.sort_by(|a, b| (a + b).cmp(&b));
|
||||
// negative examples - Not of a primitive type
|
||||
let mut vec_of_complex = vec![String::from("hello"), String::from("world!")];
|
||||
vec_of_complex.sort();
|
||||
vec_of_complex.sort_by_key(String::len);
|
||||
let mut vec = vec![(String::from("hello"), String::from("world"))];
|
||||
vec.sort();
|
||||
let mut vec = vec![[String::from("hello"), String::from("world")]];
|
||||
vec.sort();
|
||||
}
|
||||
32
tests/ui/stable_sort_primitive.rs
Normal file
32
tests/ui/stable_sort_primitive.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// run-rustfix
|
||||
#![warn(clippy::stable_sort_primitive)]
|
||||
|
||||
fn main() {
|
||||
// positive examples
|
||||
let mut vec = vec![1, 3, 2];
|
||||
vec.sort();
|
||||
let mut vec = vec![false, false, true];
|
||||
vec.sort();
|
||||
let mut vec = vec!['a', 'A', 'c'];
|
||||
vec.sort();
|
||||
let mut vec = vec!["ab", "cd", "ab", "bc"];
|
||||
vec.sort();
|
||||
let mut vec = vec![(2, 1), (1, 2), (2, 5)];
|
||||
vec.sort();
|
||||
let mut vec = vec![[2, 1], [1, 2], [2, 5]];
|
||||
vec.sort();
|
||||
let mut arr = [1, 3, 2];
|
||||
arr.sort();
|
||||
// Negative examples: behavior changes if made unstable
|
||||
let mut vec = vec![1, 3, 2];
|
||||
vec.sort_by_key(|i| i / 2);
|
||||
vec.sort_by(|a, b| (a + b).cmp(&b));
|
||||
// negative examples - Not of a primitive type
|
||||
let mut vec_of_complex = vec![String::from("hello"), String::from("world!")];
|
||||
vec_of_complex.sort();
|
||||
vec_of_complex.sort_by_key(String::len);
|
||||
let mut vec = vec![(String::from("hello"), String::from("world"))];
|
||||
vec.sort();
|
||||
let mut vec = vec![[String::from("hello"), String::from("world")]];
|
||||
vec.sort();
|
||||
}
|
||||
46
tests/ui/stable_sort_primitive.stderr
Normal file
46
tests/ui/stable_sort_primitive.stderr
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
error: Use sort_unstable instead of sort
|
||||
--> $DIR/stable_sort_primitive.rs:7:5
|
||||
|
|
||||
LL | vec.sort();
|
||||
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
||||
|
|
||||
= note: `-D clippy::stable-sort-primitive` implied by `-D warnings`
|
||||
|
||||
error: Use sort_unstable instead of sort
|
||||
--> $DIR/stable_sort_primitive.rs:9:5
|
||||
|
|
||||
LL | vec.sort();
|
||||
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
||||
|
||||
error: Use sort_unstable instead of sort
|
||||
--> $DIR/stable_sort_primitive.rs:11:5
|
||||
|
|
||||
LL | vec.sort();
|
||||
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
||||
|
||||
error: Use sort_unstable instead of sort
|
||||
--> $DIR/stable_sort_primitive.rs:13:5
|
||||
|
|
||||
LL | vec.sort();
|
||||
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
||||
|
||||
error: Use sort_unstable instead of sort
|
||||
--> $DIR/stable_sort_primitive.rs:15:5
|
||||
|
|
||||
LL | vec.sort();
|
||||
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
||||
|
||||
error: Use sort_unstable instead of sort
|
||||
--> $DIR/stable_sort_primitive.rs:17:5
|
||||
|
|
||||
LL | vec.sort();
|
||||
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
||||
|
||||
error: Use sort_unstable instead of sort
|
||||
--> $DIR/stable_sort_primitive.rs:19:5
|
||||
|
|
||||
LL | arr.sort();
|
||||
| ^^^^^^^^^^ help: try: `arr.sort_unstable()`
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
// run-rustfix
|
||||
|
||||
#![allow(clippy::stable_sort_primitive)]
|
||||
|
||||
use std::cmp::Reverse;
|
||||
|
||||
fn unnecessary_sort_by() {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
// run-rustfix
|
||||
|
||||
#![allow(clippy::stable_sort_primitive)]
|
||||
|
||||
use std::cmp::Reverse;
|
||||
|
||||
fn unnecessary_sort_by() {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
error: use Vec::sort here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:12:5
|
||||
--> $DIR/unnecessary_sort_by.rs:14:5
|
||||
|
|
||||
LL | vec.sort_by(|a, b| a.cmp(b));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort()`
|
||||
|
|
@ -7,37 +7,37 @@ 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:13: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:14: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:15: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:17:5
|
||||
--> $DIR/unnecessary_sort_by.rs:19:5
|
||||
|
|
||||
LL | vec.sort_by(|a, b| b.cmp(a));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort_by_key(|&b| Reverse(b))`
|
||||
|
||||
error: use Vec::sort_by_key here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:18: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()))`
|
||||
|
||||
error: use Vec::sort_by_key here instead
|
||||
--> $DIR/unnecessary_sort_by.rs:19: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)))`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue