Skip to content

Commit 07886a9

Browse files
committed
Detect also when works
1 parent 059e8ed commit 07886a9

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

clippy_lints/src/sort_by_key.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::utils::paths;
33
use crate::utils::sugg::Sugg;
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
6-
use rustc_hir::{Expr, ExprKind, Mutability, Param, Pat, PatKind, Path, QPath};
6+
use rustc_hir::{Expr, ExprKind, Mutability, Param, Pat, PatKind, Path, PathSegment, QPath};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99
use rustc_span::symbol::Ident;
@@ -16,7 +16,7 @@ declare_clippy_lint! {
1616
/// **Why is this bad?**
1717
/// It is more clear to use `Vec::sort_by_key` (or
1818
/// `Vec::sort_by_key` and `std::cmp::Reverse` if necessary) than
19-
/// using
19+
/// using
2020
///
2121
/// **Known problems:** None.
2222
///
@@ -36,7 +36,17 @@ declare_clippy_lint! {
3636

3737
declare_lint_pass!(SortByKey => [SORT_BY_KEY]);
3838

39-
struct LintTrigger {
39+
enum LintTrigger {
40+
Sort(SortDetection),
41+
SortByKey(SortByKeyDetection),
42+
}
43+
44+
struct SortDetection {
45+
vec_name: String,
46+
unstable: bool,
47+
}
48+
49+
struct SortByKeyDetection {
4050
vec_name: String,
4151
closure_arg: String,
4252
closure_body: String,
@@ -177,7 +187,18 @@ fn detect_lint(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> Option<LintTrigger>
177187
};
178188
let vec_name = Sugg::hir(cx, &args[0], "..").to_string();
179189
let unstable = name == "sort_unstable_by";
180-
Some(LintTrigger { vec_name, unstable, closure_arg, closure_body })
190+
if_chain! {
191+
if let ExprKind::Path(QPath::Resolved(_, Path {
192+
segments: [PathSegment { ident: left_name, .. }], ..
193+
})) = &left_expr.kind;
194+
if left_name == left_ident;
195+
then {
196+
Some(LintTrigger::Sort(SortDetection { vec_name, unstable }))
197+
}
198+
else {
199+
Some(LintTrigger::SortByKey(SortByKeyDetection { vec_name, unstable, closure_arg, closure_body }))
200+
}
201+
}
181202
} else {
182203
None
183204
}
@@ -186,8 +207,8 @@ fn detect_lint(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> Option<LintTrigger>
186207

187208
impl LateLintPass<'_, '_> for SortByKey {
188209
fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
189-
if let Some(trigger) = detect_lint(cx, expr) {
190-
utils::span_lint_and_sugg(
210+
match detect_lint(cx, expr) {
211+
Some(LintTrigger::SortByKey(trigger)) => utils::span_lint_and_sugg(
191212
cx,
192213
SORT_BY_KEY,
193214
expr.span,
@@ -201,7 +222,21 @@ impl LateLintPass<'_, '_> for SortByKey {
201222
trigger.closure_body,
202223
),
203224
Applicability::MachineApplicable,
204-
);
225+
),
226+
Some(LintTrigger::Sort(trigger)) => utils::span_lint_and_sugg(
227+
cx,
228+
SORT_BY_KEY,
229+
expr.span,
230+
"use Vec::sort here instead",
231+
"try",
232+
format!(
233+
"{}.sort{}()",
234+
trigger.vec_name,
235+
if trigger.unstable { "_unstable" } else { "" },
236+
),
237+
Applicability::MachineApplicable,
238+
),
239+
None => {},
205240
}
206241
}
207242
}

tests/ui/sort_by_key.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn id(x: isize) -> isize {
1010
fn main() {
1111
let mut vec: Vec<isize> = vec![3, 6, 1, 2, 5];
1212
// Forward examples
13-
vec.sort_by_key(|&a| a);
13+
vec.sort();
1414
vec.sort_by_key(|&a| (a + 5).abs());
1515
vec.sort_by_key(|&a| id(-a));
1616
// Reverse examples

tests/ui/sort_by_key.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: use Vec::sort_by_key here instead
1+
error: use Vec::sort here instead
22
--> $DIR/sort_by_key.rs:13:5
33
|
44
LL | vec.sort_by(|a, b| a.cmp(b));
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort_by_key(|&a| a)`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.sort()`
66
|
77
= note: `-D clippy::sort-by-key` implied by `-D warnings`
88

0 commit comments

Comments
 (0)