Improve the suggestion message of stable_sort_primitive.

This commit is contained in:
pastchick3 2021-01-20 20:05:25 +08:00
parent e6665e42a3
commit e42208f1b7
2 changed files with 39 additions and 19 deletions

View File

@ -1,4 +1,4 @@
use crate::utils::{is_slice_of_primitives, span_lint_and_sugg, sugg::Sugg}; use crate::utils::{is_slice_of_primitives, span_lint_and_then, sugg::Sugg};
use if_chain::if_chain; use if_chain::if_chain;
@ -107,25 +107,32 @@ fn detect_stable_sort_primitive(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option
impl LateLintPass<'_> for StableSortPrimitive { impl LateLintPass<'_> for StableSortPrimitive {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
if let Some(detection) = detect_stable_sort_primitive(cx, expr) { if let Some(detection) = detect_stable_sort_primitive(cx, expr) {
span_lint_and_sugg( span_lint_and_then(
cx, cx,
STABLE_SORT_PRIMITIVE, STABLE_SORT_PRIMITIVE,
expr.span, expr.span,
format!( format!(
"used {} instead of {} to sort primitive type `{}`", "used `{}` on primitive type `{}`",
detection.method.stable_name(), detection.method.stable_name(),
detection.method.unstable_name(),
detection.slice_type, detection.slice_type,
) )
.as_str(), .as_str(),
"try", |diag| {
format!( diag.span_suggestion(
"{}.{}({})", expr.span,
detection.slice_name, "try",
detection.method.unstable_name(), format!(
detection.method_args "{}.{}({})",
), detection.slice_name,
Applicability::MachineApplicable, detection.method.unstable_name(),
detection.method_args,
),
Applicability::MachineApplicable,
);
diag.note(
"an unstable sort would perform faster without any observable difference for this data type",
);
},
); );
} }
} }

View File

@ -1,46 +1,59 @@
error: used sort instead of sort_unstable to sort primitive type `i32` error: used `sort` on primitive type `i32`
--> $DIR/stable_sort_primitive.rs:7:5 --> $DIR/stable_sort_primitive.rs:7:5
| |
LL | vec.sort(); LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()` | ^^^^^^^^^^ help: try: `vec.sort_unstable()`
| |
= note: `-D clippy::stable-sort-primitive` implied by `-D warnings` = note: `-D clippy::stable-sort-primitive` implied by `-D warnings`
= note: an unstable sort would perform faster without any observable difference for this data type
error: used sort instead of sort_unstable to sort primitive type `bool` error: used `sort` on primitive type `bool`
--> $DIR/stable_sort_primitive.rs:9:5 --> $DIR/stable_sort_primitive.rs:9:5
| |
LL | vec.sort(); LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()` | ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort would perform faster without any observable difference for this data type
error: used sort instead of sort_unstable to sort primitive type `char` error: used `sort` on primitive type `char`
--> $DIR/stable_sort_primitive.rs:11:5 --> $DIR/stable_sort_primitive.rs:11:5
| |
LL | vec.sort(); LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()` | ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort would perform faster without any observable difference for this data type
error: used sort instead of sort_unstable to sort primitive type `str` error: used `sort` on primitive type `str`
--> $DIR/stable_sort_primitive.rs:13:5 --> $DIR/stable_sort_primitive.rs:13:5
| |
LL | vec.sort(); LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()` | ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort would perform faster without any observable difference for this data type
error: used sort instead of sort_unstable to sort primitive type `tuple` error: used `sort` on primitive type `tuple`
--> $DIR/stable_sort_primitive.rs:15:5 --> $DIR/stable_sort_primitive.rs:15:5
| |
LL | vec.sort(); LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()` | ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort would perform faster without any observable difference for this data type
error: used sort instead of sort_unstable to sort primitive type `array` error: used `sort` on primitive type `array`
--> $DIR/stable_sort_primitive.rs:17:5 --> $DIR/stable_sort_primitive.rs:17:5
| |
LL | vec.sort(); LL | vec.sort();
| ^^^^^^^^^^ help: try: `vec.sort_unstable()` | ^^^^^^^^^^ help: try: `vec.sort_unstable()`
|
= note: an unstable sort would perform faster without any observable difference for this data type
error: used sort instead of sort_unstable to sort primitive type `i32` error: used `sort` on primitive type `i32`
--> $DIR/stable_sort_primitive.rs:19:5 --> $DIR/stable_sort_primitive.rs:19:5
| |
LL | arr.sort(); LL | arr.sort();
| ^^^^^^^^^^ help: try: `arr.sort_unstable()` | ^^^^^^^^^^ help: try: `arr.sort_unstable()`
|
= note: an unstable sort would perform faster without any observable difference for this data type
error: aborting due to 7 previous errors error: aborting due to 7 previous errors