]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/unnecessary_sort_by.fixed
Merge commit '5034d47f721ff4c3a3ff2aca9ef2ef3e1d067f9f' into clippyup
[rust.git] / src / tools / clippy / tests / ui / unnecessary_sort_by.fixed
1 // run-rustfix
2
3 #![allow(clippy::stable_sort_primitive)]
4
5 use std::cmp::Reverse;
6
7 fn unnecessary_sort_by() {
8     fn id(x: isize) -> isize {
9         x
10     }
11
12     let mut vec: Vec<isize> = vec![3, 6, 1, 2, 5];
13     // Forward examples
14     vec.sort();
15     vec.sort_unstable();
16     vec.sort_by_key(|&a| (a + 5).abs());
17     vec.sort_unstable_by_key(|&a| id(-a));
18     // Reverse examples
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)
23     let c = &7;
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));
28
29     // Ignore vectors of references
30     let mut vec: Vec<&&&isize> = vec![&&&3, &&&6, &&&1, &&&2, &&&5];
31     vec.sort_by(|a, b| (***a).abs().cmp(&(***b).abs()));
32     vec.sort_unstable_by(|a, b| (***a).abs().cmp(&(***b).abs()));
33     vec.sort_by(|a, b| b.cmp(a));
34     vec.sort_unstable_by(|a, b| b.cmp(a));
35 }
36
37 // Do not suggest returning a reference to the closure parameter of `Vec::sort_by_key`
38 mod issue_5754 {
39     #[derive(Clone, Copy)]
40     struct Test(usize);
41
42     #[derive(PartialOrd, Ord, PartialEq, Eq)]
43     struct Wrapper<'a>(&'a usize);
44
45     impl Test {
46         fn name(&self) -> &usize {
47             &self.0
48         }
49
50         fn wrapped(&self) -> Wrapper<'_> {
51             Wrapper(&self.0)
52         }
53     }
54
55     pub fn test() {
56         let mut args: Vec<Test> = vec![];
57
58         // Forward
59         args.sort_by(|a, b| a.name().cmp(b.name()));
60         args.sort_by(|a, b| a.wrapped().cmp(&b.wrapped()));
61         args.sort_unstable_by(|a, b| a.name().cmp(b.name()));
62         args.sort_unstable_by(|a, b| a.wrapped().cmp(&b.wrapped()));
63         // Reverse
64         args.sort_by(|a, b| b.name().cmp(a.name()));
65         args.sort_by(|a, b| b.wrapped().cmp(&a.wrapped()));
66         args.sort_unstable_by(|a, b| b.name().cmp(a.name()));
67         args.sort_unstable_by(|a, b| b.wrapped().cmp(&a.wrapped()));
68     }
69 }
70
71 // `Vec::sort_by_key` closure parameter is `F: FnMut(&T) -> K`
72 // The suggestion is destructuring T and we know T is not a reference, so test that non-Copy T are
73 // not linted.
74 mod issue_6001 {
75     struct Test(String);
76
77     impl Test {
78         // Return an owned type so that we don't hit the fix for 5754
79         fn name(&self) -> String {
80             self.0.clone()
81         }
82     }
83
84     pub fn test() {
85         let mut args: Vec<Test> = vec![];
86
87         // Forward
88         args.sort_by(|a, b| a.name().cmp(&b.name()));
89         args.sort_unstable_by(|a, b| a.name().cmp(&b.name()));
90         // Reverse
91         args.sort_by(|a, b| b.name().cmp(&a.name()));
92         args.sort_unstable_by(|a, b| b.name().cmp(&a.name()));
93     }
94 }
95
96 fn main() {
97     unnecessary_sort_by();
98     issue_5754::test();
99     issue_6001::test();
100 }