3 #![allow(clippy::stable_sort_primitive)]
7 fn unnecessary_sort_by() {
8 fn id(x: isize) -> isize {
12 let mut vec: Vec<isize> = vec![3, 6, 1, 2, 5];
16 vec.sort_by_key(|&a| (a + 5).abs());
17 vec.sort_unstable_by_key(|&a| id(-a));
19 vec.sort_by_key(|&b| Reverse(b));
20 vec.sort_by_key(|&b| Reverse((b + 5).abs()));
21 vec.sort_unstable_by_key(|&b| Reverse(id(-b)));
22 // Negative examples (shouldn't be changed)
24 vec.sort_by(|a, b| (b - a).cmp(&(a - b)));
25 vec.sort_by(|_, b| b.cmp(&5));
26 vec.sort_by(|_, b| b.cmp(c));
27 vec.sort_unstable_by(|a, _| a.cmp(c));
30 // Should not be linted to avoid hitting https://github.com/rust-lang/rust/issues/34162
34 #[derive(PartialOrd, Ord, PartialEq, Eq)]
35 struct Wrapper<'a>(&'a str);
38 fn name(&self) -> &str {
42 fn wrapped(&self) -> Wrapper<'_> {
48 let mut args: Vec<Test> = vec![];
51 args.sort_by(|a, b| a.name().cmp(b.name()));
52 args.sort_by(|a, b| a.wrapped().cmp(&b.wrapped()));
53 args.sort_unstable_by(|a, b| a.name().cmp(b.name()));
54 args.sort_unstable_by(|a, b| a.wrapped().cmp(&b.wrapped()));
56 args.sort_by(|a, b| b.name().cmp(a.name()));
57 args.sort_by(|a, b| b.wrapped().cmp(&a.wrapped()));
58 args.sort_unstable_by(|a, b| b.name().cmp(a.name()));
59 args.sort_unstable_by(|a, b| b.wrapped().cmp(&a.wrapped()));
64 unnecessary_sort_by();