1 use crate::utils::{is_slice_of_primitives, sugg::Sugg};
2 use clippy_utils::diagnostics::span_lint_and_then;
4 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir::{Expr, ExprKind};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
11 declare_clippy_lint! {
13 /// When sorting primitive values (integers, bools, chars, as well
14 /// as arrays, slices, and tuples of such items), it is better to
15 /// use an unstable sort than a stable sort.
17 /// **Why is this bad?**
18 /// Using a stable sort consumes more memory and cpu cycles. Because
19 /// values which compare equal are identical, preserving their
20 /// relative order (the guarantee that a stable sort provides) means
21 /// nothing, while the extra costs still apply.
23 /// **Known problems:**
29 /// let mut vec = vec![2, 1, 3];
34 /// let mut vec = vec![2, 1, 3];
35 /// vec.sort_unstable();
37 pub STABLE_SORT_PRIMITIVE,
39 "use of sort() when sort_unstable() is equivalent"
42 declare_lint_pass!(StableSortPrimitive => [STABLE_SORT_PRIMITIVE]);
44 /// The three "kinds" of sorts
47 /* The other kinds of lint are currently commented out because they
48 * can map distinct values to equal ones. If the key function is
49 * provably one-to-one, or if the Cmp function conserves equality,
50 * then they could be linted on, but I don't know if we can check
57 /// The name of the stable version of this kind of sort
58 fn stable_name(&self) -> &str {
60 SortingKind::Vanilla => "sort",
61 /* SortingKind::ByKey => "sort_by_key",
62 * SortingKind::ByCmp => "sort_by", */
65 /// The name of the unstable version of this kind of sort
66 fn unstable_name(&self) -> &str {
68 SortingKind::Vanilla => "sort_unstable",
69 /* SortingKind::ByKey => "sort_unstable_by_key",
70 * SortingKind::ByCmp => "sort_unstable_by", */
73 /// Takes the name of a function call and returns the kind of sort
74 /// that corresponds to that function name (or None if it isn't)
75 fn from_stable_name(name: &str) -> Option<SortingKind> {
77 "sort" => Some(SortingKind::Vanilla),
78 // "sort_by" => Some(SortingKind::ByCmp),
79 // "sort_by_key" => Some(SortingKind::ByKey),
85 /// A detected instance of this lint
86 struct LintDetection {
93 fn detect_stable_sort_primitive(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<LintDetection> {
95 if let ExprKind::MethodCall(method_name, _, args, _) = &expr.kind;
96 if let Some(slice) = &args.get(0);
97 if let Some(method) = SortingKind::from_stable_name(&method_name.ident.name.as_str());
98 if let Some(slice_type) = is_slice_of_primitives(cx, slice);
100 let args_str = args.iter().skip(1).map(|arg| Sugg::hir(cx, arg, "..").to_string()).collect::<Vec<String>>().join(", ");
101 Some(LintDetection { slice_name: Sugg::hir(cx, slice, "..").to_string(), method, method_args: args_str, slice_type })
108 impl LateLintPass<'_> for StableSortPrimitive {
109 fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
110 if let Some(detection) = detect_stable_sort_primitive(cx, expr) {
113 STABLE_SORT_PRIMITIVE,
116 "used `{}` on primitive type `{}`",
117 detection.method.stable_name(),
118 detection.slice_type,
122 diag.span_suggestion(
127 detection.slice_name,
128 detection.method.unstable_name(),
129 detection.method_args,
131 Applicability::MachineApplicable,
134 "an unstable sort would perform faster without any observable difference for this data type",