Skip to content

Commit daad592

Browse files
committed
Auto merge of #6006 - ebroto:6001_unnecessary_sort_by, r=Manishearth
Restrict unnecessary_sort_by to non-reference, Copy types `Vec::sort_by_key` closure parameter is `F: FnMut(&T) -> K`. The lint's suggestion destructures the `T` parameter; this was probably done to avoid different unnamed lifetimes when `K = Reverse<&T>`. This change fixes two issues: * Destructuring T when T is non-reference requires the type to be Copy, otherwise we would try to move from a shared reference. We make sure `T: Copy` holds. * Make sure `T` is actually non-reference. I didn't go for destructuring multiple levels of references, as we would have to compensate in the closure body by removing derefs and maybe adding parens, which would add more complexity. changelog: Restrict [`unnecessary_sort_by`] to non-reference, Copy types Fixes #6001
2 parents e9440cb + 9fa9208 commit daad592

File tree

3 files changed

+87
-10
lines changed

3 files changed

+87
-10
lines changed

clippy_lints/src/unnecessary_sort_by.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::utils;
2-
use crate::utils::paths;
32
use crate::utils::sugg::Sugg;
43
use if_chain::if_chain;
54
use rustc_errors::Applicability;
@@ -171,12 +170,22 @@ fn mirrored_exprs(
171170
}
172171

173172
fn detect_lint(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<LintTrigger> {
173+
// NOTE: Vectors of references are not supported. In order to avoid hitting https://github.com/rust-lang/rust/issues/34162,
174+
// (different unnamed lifetimes for closure arg and return type) we need to make sure the suggested
175+
// closure parameter is not a reference in case we suggest `Reverse`. Trying to destructure more
176+
// than one level of references would add some extra complexity as we would have to compensate
177+
// in the closure body.
178+
174179
if_chain! {
175180
if let ExprKind::MethodCall(name_ident, _, args, _) = &expr.kind;
176181
if let name = name_ident.ident.name.to_ident_string();
177182
if name == "sort_by" || name == "sort_unstable_by";
178183
if let [vec, Expr { kind: ExprKind::Closure(_, _, closure_body_id, _, _), .. }] = args;
179-
if utils::match_type(cx, &cx.typeck_results().expr_ty(vec), &paths::VEC);
184+
let vec_ty = cx.typeck_results().expr_ty(vec);
185+
if utils::is_type_diagnostic_item(cx, vec_ty, sym!(vec_type));
186+
let ty = vec_ty.walk().nth(1).unwrap().expect_ty(); // T in Vec<T>
187+
if !matches!(&ty.kind(), ty::Ref(..));
188+
if utils::is_copy(cx, ty);
180189
if let closure_body = cx.tcx.hir().body(*closure_body_id);
181190
if let &[
182191
Param { pat: Pat { kind: PatKind::Binding(_, _, left_ident, _), .. }, ..},

tests/ui/unnecessary_sort_by.fixed

+38-4
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,25 @@ fn unnecessary_sort_by() {
2525
vec.sort_by(|_, b| b.cmp(&5));
2626
vec.sort_by(|_, b| b.cmp(c));
2727
vec.sort_unstable_by(|a, _| a.cmp(c));
28+
29+
// Ignore vectors of references
30+
let mut vec: Vec<&&&isize> = vec![&&&3, &&&6, &&&1, &&&2, &&&5];
31+
vec.sort_by(|a, b| (***a).abs().cmp(&(***b).abs()));
32+
vec.sort_unstable_by(|a, b| (***a).abs().cmp(&(***b).abs()));
33+
vec.sort_by(|a, b| b.cmp(a));
34+
vec.sort_unstable_by(|a, b| b.cmp(a));
2835
}
2936

30-
// Should not be linted to avoid hitting https://github.com/rust-lang/rust/issues/34162
37+
// Do not suggest returning a reference to the closure parameter of `Vec::sort_by_key`
3138
mod issue_5754 {
32-
struct Test(String);
39+
#[derive(Clone, Copy)]
40+
struct Test(usize);
3341

3442
#[derive(PartialOrd, Ord, PartialEq, Eq)]
35-
struct Wrapper<'a>(&'a str);
43+
struct Wrapper<'a>(&'a usize);
3644

3745
impl Test {
38-
fn name(&self) -> &str {
46+
fn name(&self) -> &usize {
3947
&self.0
4048
}
4149

@@ -60,7 +68,33 @@ mod issue_5754 {
6068
}
6169
}
6270

71+
// `Vec::sort_by_key` closure parameter is `F: FnMut(&T) -> K`
72+
// The suggestion is destructuring T and we know T is not a reference, so test that non-Copy T are
73+
// not linted.
74+
mod issue_6001 {
75+
struct Test(String);
76+
77+
impl Test {
78+
// Return an owned type so that we don't hit the fix for 5754
79+
fn name(&self) -> String {
80+
self.0.clone()
81+
}
82+
}
83+
84+
pub fn test() {
85+
let mut args: Vec<Test> = vec![];
86+
87+
// Forward
88+
args.sort_by(|a, b| a.name().cmp(&b.name()));
89+
args.sort_unstable_by(|a, b| a.name().cmp(&b.name()));
90+
// Reverse
91+
args.sort_by(|a, b| b.name().cmp(&a.name()));
92+
args.sort_unstable_by(|a, b| b.name().cmp(&a.name()));
93+
}
94+
}
95+
6396
fn main() {
6497
unnecessary_sort_by();
6598
issue_5754::test();
99+
issue_6001::test();
66100
}

tests/ui/unnecessary_sort_by.rs

+38-4
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,25 @@ fn unnecessary_sort_by() {
2525
vec.sort_by(|_, b| b.cmp(&5));
2626
vec.sort_by(|_, b| b.cmp(c));
2727
vec.sort_unstable_by(|a, _| a.cmp(c));
28+
29+
// Ignore vectors of references
30+
let mut vec: Vec<&&&isize> = vec![&&&3, &&&6, &&&1, &&&2, &&&5];
31+
vec.sort_by(|a, b| (***a).abs().cmp(&(***b).abs()));
32+
vec.sort_unstable_by(|a, b| (***a).abs().cmp(&(***b).abs()));
33+
vec.sort_by(|a, b| b.cmp(a));
34+
vec.sort_unstable_by(|a, b| b.cmp(a));
2835
}
2936

30-
// Should not be linted to avoid hitting https://github.com/rust-lang/rust/issues/34162
37+
// Do not suggest returning a reference to the closure parameter of `Vec::sort_by_key`
3138
mod issue_5754 {
32-
struct Test(String);
39+
#[derive(Clone, Copy)]
40+
struct Test(usize);
3341

3442
#[derive(PartialOrd, Ord, PartialEq, Eq)]
35-
struct Wrapper<'a>(&'a str);
43+
struct Wrapper<'a>(&'a usize);
3644

3745
impl Test {
38-
fn name(&self) -> &str {
46+
fn name(&self) -> &usize {
3947
&self.0
4048
}
4149

@@ -60,7 +68,33 @@ mod issue_5754 {
6068
}
6169
}
6270

71+
// `Vec::sort_by_key` closure parameter is `F: FnMut(&T) -> K`
72+
// The suggestion is destructuring T and we know T is not a reference, so test that non-Copy T are
73+
// not linted.
74+
mod issue_6001 {
75+
struct Test(String);
76+
77+
impl Test {
78+
// Return an owned type so that we don't hit the fix for 5754
79+
fn name(&self) -> String {
80+
self.0.clone()
81+
}
82+
}
83+
84+
pub fn test() {
85+
let mut args: Vec<Test> = vec![];
86+
87+
// Forward
88+
args.sort_by(|a, b| a.name().cmp(&b.name()));
89+
args.sort_unstable_by(|a, b| a.name().cmp(&b.name()));
90+
// Reverse
91+
args.sort_by(|a, b| b.name().cmp(&a.name()));
92+
args.sort_unstable_by(|a, b| b.name().cmp(&a.name()));
93+
}
94+
}
95+
6396
fn main() {
6497
unnecessary_sort_by();
6598
issue_5754::test();
99+
issue_6001::test();
66100
}

0 commit comments

Comments
 (0)