]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/stable_sort_primitive.rs
Rollup merge of #83478 - jyn514:fine-grained-files, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / clippy_lints / src / stable_sort_primitive.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::{is_slice_of_primitives, sugg::Sugg};
3 use if_chain::if_chain;
4 use rustc_errors::Applicability;
5 use rustc_hir::{Expr, ExprKind};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8
9 declare_clippy_lint! {
10     /// **What it does:**
11     /// When sorting primitive values (integers, bools, chars, as well
12     /// as arrays, slices, and tuples of such items), it is better to
13     /// use an unstable sort than a stable sort.
14     ///
15     /// **Why is this bad?**
16     /// Using a stable sort consumes more memory and cpu cycles. Because
17     /// values which compare equal are identical, preserving their
18     /// relative order (the guarantee that a stable sort provides) means
19     /// nothing, while the extra costs still apply.
20     ///
21     /// **Known problems:**
22     /// None
23     ///
24     /// **Example:**
25     ///
26     /// ```rust
27     /// let mut vec = vec![2, 1, 3];
28     /// vec.sort();
29     /// ```
30     /// Use instead:
31     /// ```rust
32     /// let mut vec = vec![2, 1, 3];
33     /// vec.sort_unstable();
34     /// ```
35     pub STABLE_SORT_PRIMITIVE,
36     perf,
37     "use of sort() when sort_unstable() is equivalent"
38 }
39
40 declare_lint_pass!(StableSortPrimitive => [STABLE_SORT_PRIMITIVE]);
41
42 /// The three "kinds" of sorts
43 enum SortingKind {
44     Vanilla,
45     /* The other kinds of lint are currently commented out because they
46      * can map distinct values to equal ones. If the key function is
47      * provably one-to-one, or if the Cmp function conserves equality,
48      * then they could be linted on, but I don't know if we can check
49      * for that. */
50
51     /* ByKey,
52      * ByCmp, */
53 }
54 impl SortingKind {
55     /// The name of the stable version of this kind of sort
56     fn stable_name(&self) -> &str {
57         match self {
58             SortingKind::Vanilla => "sort",
59             /* SortingKind::ByKey => "sort_by_key",
60              * SortingKind::ByCmp => "sort_by", */
61         }
62     }
63     /// The name of the unstable version of this kind of sort
64     fn unstable_name(&self) -> &str {
65         match self {
66             SortingKind::Vanilla => "sort_unstable",
67             /* SortingKind::ByKey => "sort_unstable_by_key",
68              * SortingKind::ByCmp => "sort_unstable_by", */
69         }
70     }
71     /// Takes the name of a function call and returns the kind of sort
72     /// that corresponds to that function name (or None if it isn't)
73     fn from_stable_name(name: &str) -> Option<SortingKind> {
74         match name {
75             "sort" => Some(SortingKind::Vanilla),
76             // "sort_by" => Some(SortingKind::ByCmp),
77             // "sort_by_key" => Some(SortingKind::ByKey),
78             _ => None,
79         }
80     }
81 }
82
83 /// A detected instance of this lint
84 struct LintDetection {
85     slice_name: String,
86     method: SortingKind,
87     method_args: String,
88     slice_type: String,
89 }
90
91 fn detect_stable_sort_primitive(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<LintDetection> {
92     if_chain! {
93         if let ExprKind::MethodCall(method_name, _, args, _) = &expr.kind;
94         if let Some(slice) = &args.get(0);
95         if let Some(method) = SortingKind::from_stable_name(&method_name.ident.name.as_str());
96         if let Some(slice_type) = is_slice_of_primitives(cx, slice);
97         then {
98             let args_str = args.iter().skip(1).map(|arg| Sugg::hir(cx, arg, "..").to_string()).collect::<Vec<String>>().join(", ");
99             Some(LintDetection { slice_name: Sugg::hir(cx, slice, "..").to_string(), method, method_args: args_str, slice_type })
100         } else {
101             None
102         }
103     }
104 }
105
106 impl LateLintPass<'_> for StableSortPrimitive {
107     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
108         if let Some(detection) = detect_stable_sort_primitive(cx, expr) {
109             span_lint_and_then(
110                 cx,
111                 STABLE_SORT_PRIMITIVE,
112                 expr.span,
113                 format!(
114                     "used `{}` on primitive type `{}`",
115                     detection.method.stable_name(),
116                     detection.slice_type,
117                 )
118                 .as_str(),
119                 |diag| {
120                     diag.span_suggestion(
121                         expr.span,
122                         "try",
123                         format!(
124                             "{}.{}({})",
125                             detection.slice_name,
126                             detection.method.unstable_name(),
127                             detection.method_args,
128                         ),
129                         Applicability::MachineApplicable,
130                     );
131                     diag.note(
132                         "an unstable sort would perform faster without any observable difference for this data type",
133                     );
134                 },
135             );
136         }
137     }
138 }