Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e42208f

Browse files
committed
Improve the suggestion message of stable_sort_primitive.
1 parent e6665e4 commit e42208f

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

clippy_lints/src/stable_sort_primitive.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{is_slice_of_primitives, span_lint_and_sugg, sugg::Sugg};
1+
use crate::utils::{is_slice_of_primitives, span_lint_and_then, sugg::Sugg};
22

33
use if_chain::if_chain;
44

@@ -107,25 +107,32 @@ fn detect_stable_sort_primitive(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option
107107
impl LateLintPass<'_> for StableSortPrimitive {
108108
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
109109
if let Some(detection) = detect_stable_sort_primitive(cx, expr) {
110-
span_lint_and_sugg(
110+
span_lint_and_then(
111111
cx,
112112
STABLE_SORT_PRIMITIVE,
113113
expr.span,
114114
format!(
115-
"used {} instead of {} to sort primitive type `{}`",
115+
"used `{}` on primitive type `{}`",
116116
detection.method.stable_name(),
117-
detection.method.unstable_name(),
118117
detection.slice_type,
119118
)
120119
.as_str(),
121-
"try",
122-
format!(
123-
"{}.{}({})",
124-
detection.slice_name,
125-
detection.method.unstable_name(),
126-
detection.method_args
127-
),
128-
Applicability::MachineApplicable,
120+
|diag| {
121+
diag.span_suggestion(
122+
expr.span,
123+
"try",
124+
format!(
125+
"{}.{}({})",
126+
detection.slice_name,
127+
detection.method.unstable_name(),
128+
detection.method_args,
129+
),
130+
Applicability::MachineApplicable,
131+
);
132+
diag.note(
133+
"an unstable sort would perform faster without any observable difference for this data type",
134+
);
135+
},
129136
);
130137
}
131138
}

tests/ui/stable_sort_primitive.stderr

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,59 @@
1-
error: used sort instead of sort_unstable to sort primitive type `i32`
1+
error: used `sort` on primitive type `i32`
22
--> $DIR/stable_sort_primitive.rs:7:5
33
|
44
LL | vec.sort();
55
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
66
|
77
= note: `-D clippy::stable-sort-primitive` implied by `-D warnings`
8+
= note: an unstable sort would perform faster without any observable difference for this data type
89

9-
error: used sort instead of sort_unstable to sort primitive type `bool`
10+
error: used `sort` on primitive type `bool`
1011
--> $DIR/stable_sort_primitive.rs:9:5
1112
|
1213
LL | vec.sort();
1314
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
15+
|
16+
= note: an unstable sort would perform faster without any observable difference for this data type
1417

15-
error: used sort instead of sort_unstable to sort primitive type `char`
18+
error: used `sort` on primitive type `char`
1619
--> $DIR/stable_sort_primitive.rs:11:5
1720
|
1821
LL | vec.sort();
1922
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
23+
|
24+
= note: an unstable sort would perform faster without any observable difference for this data type
2025

21-
error: used sort instead of sort_unstable to sort primitive type `str`
26+
error: used `sort` on primitive type `str`
2227
--> $DIR/stable_sort_primitive.rs:13:5
2328
|
2429
LL | vec.sort();
2530
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
31+
|
32+
= note: an unstable sort would perform faster without any observable difference for this data type
2633

27-
error: used sort instead of sort_unstable to sort primitive type `tuple`
34+
error: used `sort` on primitive type `tuple`
2835
--> $DIR/stable_sort_primitive.rs:15:5
2936
|
3037
LL | vec.sort();
3138
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
39+
|
40+
= note: an unstable sort would perform faster without any observable difference for this data type
3241

33-
error: used sort instead of sort_unstable to sort primitive type `array`
42+
error: used `sort` on primitive type `array`
3443
--> $DIR/stable_sort_primitive.rs:17:5
3544
|
3645
LL | vec.sort();
3746
| ^^^^^^^^^^ help: try: `vec.sort_unstable()`
47+
|
48+
= note: an unstable sort would perform faster without any observable difference for this data type
3849

39-
error: used sort instead of sort_unstable to sort primitive type `i32`
50+
error: used `sort` on primitive type `i32`
4051
--> $DIR/stable_sort_primitive.rs:19:5
4152
|
4253
LL | arr.sort();
4354
| ^^^^^^^^^^ help: try: `arr.sort_unstable()`
55+
|
56+
= note: an unstable sort would perform faster without any observable difference for this data type
4457

4558
error: aborting due to 7 previous errors
4659

0 commit comments

Comments
 (0)