]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/unnecessary_sort_by.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / unnecessary_sort_by.rs
CommitLineData
f20569fa
XL
1// run-rustfix
2
3#![allow(clippy::stable_sort_primitive)]
4
5use std::cmp::Reverse;
6
7fn 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_by(|a, b| a.cmp(b));
15 vec.sort_unstable_by(|a, b| a.cmp(b));
16 vec.sort_by(|a, b| (a + 5).abs().cmp(&(b + 5).abs()));
17 vec.sort_unstable_by(|a, b| id(-a).cmp(&id(-b)));
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(|a, b| (b + 5).abs().cmp(&(a + 5).abs()));
21 vec.sort_unstable_by(|a, b| id(-b).cmp(&id(-a)));
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(|a, b| (***a).abs().cmp(&(***b).abs()));
32 vec.sort_unstable_by(|a, b| (***a).abs().cmp(&(***b).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
38// Do not suggest returning a reference to the closure parameter of `Vec::sort_by_key`
39mod issue_5754 {
40 #[derive(Clone, Copy)]
41 struct Test(usize);
42
43 #[derive(PartialOrd, Ord, PartialEq, Eq)]
44 struct Wrapper<'a>(&'a usize);
45
46 impl Test {
47 fn name(&self) -> &usize {
48 &self.0
49 }
50
51 fn wrapped(&self) -> Wrapper<'_> {
52 Wrapper(&self.0)
53 }
54 }
55
56 pub fn test() {
57 let mut args: Vec<Test> = vec![];
58
59 // Forward
60 args.sort_by(|a, b| a.name().cmp(b.name()));
61 args.sort_by(|a, b| a.wrapped().cmp(&b.wrapped()));
62 args.sort_unstable_by(|a, b| a.name().cmp(b.name()));
63 args.sort_unstable_by(|a, b| a.wrapped().cmp(&b.wrapped()));
64 // Reverse
65 args.sort_by(|a, b| b.name().cmp(a.name()));
66 args.sort_by(|a, b| b.wrapped().cmp(&a.wrapped()));
67 args.sort_unstable_by(|a, b| b.name().cmp(a.name()));
68 args.sort_unstable_by(|a, b| b.wrapped().cmp(&a.wrapped()));
69 }
70}
71
72// The closure parameter is not dereferenced anymore, so non-Copy types can be linted
73mod issue_6001 {
74 use super::*;
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
96fn main() {
97 unnecessary_sort_by();
98 issue_5754::test();
99 issue_6001::test();
100}