Skip to content

Update stable_sort_primitive to prefer sort not only primitive types #10258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4746,7 +4746,7 @@ Released 2018-09-13
[`size_of_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#size_of_ref
[`skip_while_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#skip_while_next
[`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization
[`stable_sort_primitive`]: https://rust-lang.github.io/rust-clippy/master/index.html#stable_sort_primitive
[`stable_sort`]: https://rust-lang.github.io/rust-clippy/master/index.html#stable_sort
[`std_instead_of_alloc`]: https://rust-lang.github.io/rust-clippy/master/index.html#std_instead_of_alloc
[`std_instead_of_core`]: https://rust-lang.github.io/rust-clippy/master/index.html#std_instead_of_core
[`str_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#str_to_string
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::methods::SINGLE_CHAR_ADD_STR_INFO,
crate::methods::SINGLE_CHAR_PATTERN_INFO,
crate::methods::SKIP_WHILE_NEXT_INFO,
crate::methods::STABLE_SORT_PRIMITIVE_INFO,
crate::methods::STABLE_SORT_INFO,
crate::methods::STRING_EXTEND_CHARS_INFO,
crate::methods::SUSPICIOUS_MAP_INFO,
crate::methods::SUSPICIOUS_SPLITN_INFO,
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ mod single_char_insert_string;
mod single_char_pattern;
mod single_char_push_string;
mod skip_while_next;
mod stable_sort_primitive;
mod stable_sort;
mod str_splitn;
mod string_extend_chars;
mod suspicious_map;
Expand Down Expand Up @@ -2907,7 +2907,7 @@ declare_clippy_lint! {
/// vec.sort_unstable();
/// ```
#[clippy::version = "1.47.0"]
pub STABLE_SORT_PRIMITIVE,
pub STABLE_SORT,
pedantic,
"use of sort() when sort_unstable() is equivalent"
}
Expand Down Expand Up @@ -3279,7 +3279,7 @@ impl_lint_pass!(Methods => [
PATH_BUF_PUSH_OVERWRITE,
RANGE_ZIP_WITH_LEN,
REPEAT_ONCE,
STABLE_SORT_PRIMITIVE,
STABLE_SORT,
UNIT_HASH,
UNNECESSARY_SORT_BY,
VEC_RESIZE_TO_ZERO,
Expand Down Expand Up @@ -3691,7 +3691,7 @@ impl Methods {
}
},
("sort", []) => {
stable_sort_primitive::check(cx, expr, recv);
stable_sort::check(cx, expr, recv);
},
("sort_by", [arg]) => {
unnecessary_sort_by::check(cx, expr, recv, arg, false);
Expand Down
24 changes: 24 additions & 0 deletions clippy_lints/src/methods/stable_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet_with_context;
use rustc_errors::Applicability;
use rustc_hir::Expr;
use rustc_lint::LateContext;

use super::STABLE_SORT;

pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
span_lint_and_then(
cx,
STABLE_SORT,
e.span,
&format!("used `sort` over `sort_unstable`"),
|diag| {
let mut app = Applicability::MachineApplicable;
let recv_snip = snippet_with_context(cx, recv.span, e.span.ctxt(), "..", &mut app).0;
diag.span_suggestion(e.span, "try", format!("{recv_snip}.sort_unstable()"), app);
diag.note(
"an unstable sort typically performs faster without any observable difference for this data type",
);
},
);
}
31 changes: 0 additions & 31 deletions clippy_lints/src/methods/stable_sort_primitive.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// run-rustfix
#![warn(clippy::stable_sort_primitive)]
#![warn(clippy::stable_sort)]

fn main() {
// positive examples
// Positive examples
let mut vec = vec![1, 3, 2];
vec.sort_unstable();
let mut vec = vec![false, false, true];
Expand All @@ -17,16 +17,15 @@ fn main() {
vec.sort_unstable();
let mut arr = [1, 3, 2];
arr.sort_unstable();
// Negative examples: behavior changes if made unstable
let mut vec = vec![1, 3, 2];
vec.sort_by_key(|i| i / 2);
vec.sort_by(|&a, &b| (a + b).cmp(&b));
// negative examples - Not of a primitive type
let mut vec_of_complex = vec![String::from("hello"), String::from("world!")];
vec_of_complex.sort();
vec_of_complex.sort_unstable();
vec_of_complex.sort_by_key(String::len);
let mut vec = vec![(String::from("hello"), String::from("world"))];
vec.sort();
vec.sort_unstable();
let mut vec = vec![[String::from("hello"), String::from("world")]];
vec.sort();
vec.sort_unstable();
// Negative examples: behavior changes if made unstable
let mut vec = vec![1, 3, 2];
vec.sort_by_key(|i| i / 2);
vec.sort_by(|&a, &b| (a + b).cmp(&b));
}
13 changes: 6 additions & 7 deletions tests/ui/stable_sort_primitive.rs → tests/ui/stable_sort.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// run-rustfix
#![warn(clippy::stable_sort_primitive)]
#![warn(clippy::stable_sort)]

fn main() {
// positive examples
// Positive examples
let mut vec = vec![1, 3, 2];
vec.sort();
let mut vec = vec![false, false, true];
Expand All @@ -17,16 +17,15 @@ fn main() {
vec.sort();
let mut arr = [1, 3, 2];
arr.sort();
// Negative examples: behavior changes if made unstable
let mut vec = vec![1, 3, 2];
vec.sort_by_key(|i| i / 2);
vec.sort_by(|&a, &b| (a + b).cmp(&b));
// negative examples - Not of a primitive type
let mut vec_of_complex = vec![String::from("hello"), String::from("world!")];
vec_of_complex.sort();
vec_of_complex.sort_by_key(String::len);
let mut vec = vec![(String::from("hello"), String::from("world"))];
vec.sort();
let mut vec = vec![[String::from("hello"), String::from("world")]];
vec.sort();
// Negative examples: behavior changes if made unstable
let mut vec = vec![1, 3, 2];
vec.sort_by_key(|i| i / 2);
vec.sort_by(|&a, &b| (a + b).cmp(&b));
}
83 changes: 83 additions & 0 deletions tests/ui/stable_sort.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
error: used `sort` over `sort_unstable`
--> $DIR/stable_sort.rs:7:5
|
LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort typically performs faster without any observable difference for this data type
= note: `-D clippy::stable-sort` implied by `-D warnings`

error: used `sort` over `sort_unstable`
--> $DIR/stable_sort.rs:9:5
|
LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort typically performs faster without any observable difference for this data type

error: used `sort` over `sort_unstable`
--> $DIR/stable_sort.rs:11:5
|
LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort typically performs faster without any observable difference for this data type

error: used `sort` over `sort_unstable`
--> $DIR/stable_sort.rs:13:5
|
LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort typically performs faster without any observable difference for this data type

error: used `sort` over `sort_unstable`
--> $DIR/stable_sort.rs:15:5
|
LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort typically performs faster without any observable difference for this data type

error: used `sort` over `sort_unstable`
--> $DIR/stable_sort.rs:17:5
|
LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort typically performs faster without any observable difference for this data type

error: used `sort` over `sort_unstable`
--> $DIR/stable_sort.rs:19:5
|
LL | arr.sort();
| ^^^^^^^^^^ help: try: `arr.sort_unstable()`
|
= note: an unstable sort typically performs faster without any observable difference for this data type

error: used `sort` over `sort_unstable`
--> $DIR/stable_sort.rs:21:5
|
LL | vec_of_complex.sort();
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_of_complex.sort_unstable()`
|
= note: an unstable sort typically performs faster without any observable difference for this data type

error: used `sort` over `sort_unstable`
--> $DIR/stable_sort.rs:24:5
|
LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort typically performs faster without any observable difference for this data type

error: used `sort` over `sort_unstable`
--> $DIR/stable_sort.rs:26:5
|
LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort typically performs faster without any observable difference for this data type

error: aborting due to 10 previous errors

59 changes: 0 additions & 59 deletions tests/ui/stable_sort_primitive.stderr

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ui/unnecessary_sort_by.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// run-rustfix

#![allow(clippy::stable_sort_primitive)]
#![allow(clippy::stable_sort)]

use std::cell::Ref;

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unnecessary_sort_by.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// run-rustfix

#![allow(clippy::stable_sort_primitive)]
#![allow(clippy::stable_sort)]

use std::cell::Ref;

Expand Down