]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/unnecessary_sort_by.fixed
Merge commit 'e36a20c24f35a4cee82bbdc600289104c9237c22' into ra-sync-and-pms-component
[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::cell::Ref;
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(|a, b| b.cmp(a)); // not linted to avoid suggesting `Reverse(b)` which would borrow
20     vec.sort_by_key(|b| std::cmp::Reverse((b + 5).abs()));
21     vec.sort_unstable_by_key(|b| std::cmp::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     // Vectors of references are fine as long as the resulting key does not borrow
30     let mut vec: Vec<&&&isize> = vec![&&&3, &&&6, &&&1, &&&2, &&&5];
31     vec.sort_by_key(|a| (***a).abs());
32     vec.sort_unstable_by_key(|a| (***a).abs());
33     // `Reverse(b)` would borrow in the following cases, don't lint
34     vec.sort_by(|a, b| b.cmp(a));
35     vec.sort_unstable_by(|a, b| b.cmp(a));
36
37     // No warning if element does not implement `Ord`
38     let mut vec: Vec<Ref<usize>> = Vec::new();
39     vec.sort_unstable_by(|a, b| a.cmp(b));
40 }
41
42 // Do not suggest returning a reference to the closure parameter of `Vec::sort_by_key`
43 mod issue_5754 {
44     #[derive(Clone, Copy)]
45     struct Test(usize);
46
47     #[derive(PartialOrd, Ord, PartialEq, Eq)]
48     struct Wrapper<'a>(&'a usize);
49
50     impl Test {
51         fn name(&self) -> &usize {
52             &self.0
53         }
54
55         fn wrapped(&self) -> Wrapper<'_> {
56             Wrapper(&self.0)
57         }
58     }
59
60     pub fn test() {
61         let mut args: Vec<Test> = vec![];
62
63         // Forward
64         args.sort_by(|a, b| a.name().cmp(b.name()));
65         args.sort_by(|a, b| a.wrapped().cmp(&b.wrapped()));
66         args.sort_unstable_by(|a, b| a.name().cmp(b.name()));
67         args.sort_unstable_by(|a, b| a.wrapped().cmp(&b.wrapped()));
68         // Reverse
69         args.sort_by(|a, b| b.name().cmp(a.name()));
70         args.sort_by(|a, b| b.wrapped().cmp(&a.wrapped()));
71         args.sort_unstable_by(|a, b| b.name().cmp(a.name()));
72         args.sort_unstable_by(|a, b| b.wrapped().cmp(&a.wrapped()));
73     }
74 }
75
76 // The closure parameter is not dereferenced anymore, so non-Copy types can be linted
77 mod issue_6001 {
78     struct Test(String);
79
80     impl Test {
81         // Return an owned type so that we don't hit the fix for 5754
82         fn name(&self) -> String {
83             self.0.clone()
84         }
85     }
86
87     pub fn test() {
88         let mut args: Vec<Test> = vec![];
89
90         // Forward
91         args.sort_by_key(|a| a.name());
92         args.sort_unstable_by_key(|a| a.name());
93         // Reverse
94         args.sort_by_key(|b| std::cmp::Reverse(b.name()));
95         args.sort_unstable_by_key(|b| std::cmp::Reverse(b.name()));
96     }
97 }
98
99 fn main() {
100     unnecessary_sort_by();
101     issue_5754::test();
102     issue_6001::test();
103 }