Skip to content

Commit e834855

Browse files
committed
Move StableSortPrimitive to Methods lint pass
1 parent 06d752e commit e834855

File tree

6 files changed

+76
-148
lines changed

6 files changed

+76
-148
lines changed

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ store.register_lints(&[
353353
methods::SINGLE_CHAR_ADD_STR,
354354
methods::SINGLE_CHAR_PATTERN,
355355
methods::SKIP_WHILE_NEXT,
356+
methods::STABLE_SORT_PRIMITIVE,
356357
methods::STRING_EXTEND_CHARS,
357358
methods::SUSPICIOUS_MAP,
358359
methods::SUSPICIOUS_SPLITN,
@@ -504,7 +505,6 @@ store.register_lints(&[
504505
single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS,
505506
size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT,
506507
slow_vector_initialization::SLOW_VECTOR_INITIALIZATION,
507-
stable_sort_primitive::STABLE_SORT_PRIMITIVE,
508508
std_instead_of_core::ALLOC_INSTEAD_OF_CORE,
509509
std_instead_of_core::STD_INSTEAD_OF_ALLOC,
510510
std_instead_of_core::STD_INSTEAD_OF_CORE,

clippy_lints/src/lib.register_pedantic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
6464
LintId::of(methods::MANUAL_OK_OR),
6565
LintId::of(methods::MAP_UNWRAP_OR),
6666
LintId::of(methods::NAIVE_BYTECOUNT),
67+
LintId::of(methods::STABLE_SORT_PRIMITIVE),
6768
LintId::of(methods::UNNECESSARY_JOIN),
6869
LintId::of(misc::USED_UNDERSCORE_BINDING),
6970
LintId::of(mismatching_type_param_order::MISMATCHING_TYPE_PARAM_ORDER),
@@ -85,7 +86,6 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
8586
LintId::of(ref_option_ref::REF_OPTION_REF),
8687
LintId::of(return_self_not_must_use::RETURN_SELF_NOT_MUST_USE),
8788
LintId::of(semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED),
88-
LintId::of(stable_sort_primitive::STABLE_SORT_PRIMITIVE),
8989
LintId::of(strings::STRING_ADD_ASSIGN),
9090
LintId::of(transmute::TRANSMUTE_PTR_TO_PTR),
9191
LintId::of(types::LINKEDLIST),

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ mod single_char_lifetime_names;
354354
mod single_component_path_imports;
355355
mod size_of_in_element_count;
356356
mod slow_vector_initialization;
357-
mod stable_sort_primitive;
358357
mod std_instead_of_core;
359358
mod strings;
360359
mod strlen_on_c_strings;
@@ -822,7 +821,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
822821
store.register_early_pass(move || Box::new(nonstandard_macro_braces::MacroBraces::new(&macro_matcher)));
823822
store.register_late_pass(|| Box::new(macro_use::MacroUseImports::default()));
824823
store.register_late_pass(|| Box::new(pattern_type_mismatch::PatternTypeMismatch));
825-
store.register_late_pass(|| Box::new(stable_sort_primitive::StableSortPrimitive));
826824
store.register_late_pass(|| Box::new(unwrap_in_result::UnwrapInResult));
827825
store.register_late_pass(|| Box::new(semicolon_if_nothing_returned::SemicolonIfNothingReturned));
828826
store.register_late_pass(|| Box::new(async_yields_async::AsyncYieldsAsync));

clippy_lints/src/methods/mod.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ mod single_char_insert_string;
7272
mod single_char_pattern;
7373
mod single_char_push_string;
7474
mod skip_while_next;
75+
mod stable_sort_primitive;
7576
mod str_splitn;
7677
mod string_extend_chars;
7778
mod suspicious_map;
@@ -2793,6 +2794,44 @@ declare_clippy_lint! {
27932794
"using `.repeat(1)` instead of `String.clone()`, `str.to_string()` or `slice.to_vec()` "
27942795
}
27952796

2797+
declare_clippy_lint! {
2798+
/// ### What it does
2799+
/// When sorting primitive values (integers, bools, chars, as well
2800+
/// as arrays, slices, and tuples of such items), it is typically better to
2801+
/// use an unstable sort than a stable sort.
2802+
///
2803+
/// ### Why is this bad?
2804+
/// Typically, using a stable sort consumes more memory and cpu cycles.
2805+
/// Because values which compare equal are identical, preserving their
2806+
/// relative order (the guarantee that a stable sort provides) means
2807+
/// nothing, while the extra costs still apply.
2808+
///
2809+
/// ### Known problems
2810+
///
2811+
/// As pointed out in
2812+
/// [issue #8241](https://github.com/rust-lang/rust-clippy/issues/8241),
2813+
/// a stable sort can instead be significantly faster for certain scenarios
2814+
/// (eg. when a sorted vector is extended with new data and resorted).
2815+
///
2816+
/// For more information and benchmarking results, please refer to the
2817+
/// issue linked above.
2818+
///
2819+
/// ### Example
2820+
/// ```rust
2821+
/// let mut vec = vec![2, 1, 3];
2822+
/// vec.sort();
2823+
/// ```
2824+
/// Use instead:
2825+
/// ```rust
2826+
/// let mut vec = vec![2, 1, 3];
2827+
/// vec.sort_unstable();
2828+
/// ```
2829+
#[clippy::version = "1.47.0"]
2830+
pub STABLE_SORT_PRIMITIVE,
2831+
pedantic,
2832+
"use of sort() when sort_unstable() is equivalent"
2833+
}
2834+
27962835
pub struct Methods {
27972836
avoid_breaking_exported_api: bool,
27982837
msrv: Option<RustcVersion>,
@@ -2909,6 +2948,7 @@ impl_lint_pass!(Methods => [
29092948
PATH_BUF_PUSH_OVERWRITE,
29102949
RANGE_ZIP_WITH_LEN,
29112950
REPEAT_ONCE,
2951+
STABLE_SORT_PRIMITIVE,
29122952
]);
29132953

29142954
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3300,6 +3340,9 @@ impl Methods {
33003340
("repeat", [arg]) => {
33013341
repeat_once::check(cx, expr, recv, arg);
33023342
},
3343+
("sort", []) => {
3344+
stable_sort_primitive::check(cx, expr, recv);
3345+
},
33033346
("splitn" | "rsplitn", [count_arg, pat_arg]) => {
33043347
if let Some((Constant::Int(count), _)) = constant(cx, cx.typeck_results(), count_arg) {
33053348
suspicious_splitn::check(cx, name, expr, recv, count);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::is_slice_of_primitives;
3+
use clippy_utils::source::snippet_with_context;
4+
use rustc_errors::Applicability;
5+
use rustc_hir::Expr;
6+
use rustc_lint::LateContext;
7+
8+
use super::STABLE_SORT_PRIMITIVE;
9+
10+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
11+
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
12+
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
13+
&& cx.tcx.type_of(impl_id).is_slice()
14+
&& let Some(slice_type) = is_slice_of_primitives(cx, recv)
15+
{
16+
span_lint_and_then(
17+
cx,
18+
STABLE_SORT_PRIMITIVE,
19+
e.span,
20+
&format!("used `sort` on primitive type `{}`", slice_type),
21+
|diag| {
22+
let mut app = Applicability::MachineApplicable;
23+
let recv_snip = snippet_with_context(cx, recv.span, e.span.ctxt(), "..", &mut app).0;
24+
diag.span_suggestion(e.span, "try", format!("{}.sort_unstable()", recv_snip), app);
25+
diag.note(
26+
"an unstable sort typically performs faster without any observable difference for this data type",
27+
);
28+
},
29+
);
30+
}
31+
}

clippy_lints/src/stable_sort_primitive.rs

Lines changed: 0 additions & 144 deletions
This file was deleted.

0 commit comments

Comments
 (0)