From 3d0628d1faf43db043c5f8eda3ee8bbea73dcf48 Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Sun, 2 Aug 2020 17:20:50 -0700 Subject: [PATCH] Also lint for static strings --- clippy_lints/src/stable_sort_primitive.rs | 3 ++- tests/ui/stable_sort_primitive.fixed | 2 ++ tests/ui/stable_sort_primitive.rs | 2 ++ tests/ui/stable_sort_primitive.stderr | 8 +++++++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/stable_sort_primitive.rs b/clippy_lints/src/stable_sort_primitive.rs index 21d100362cfc..448aa3f925af 100644 --- a/clippy_lints/src/stable_sort_primitive.rs +++ b/clippy_lints/src/stable_sort_primitive.rs @@ -93,7 +93,8 @@ struct LintDetection { /// on. fn is_primitive_type(ty: Ty<'_>) -> bool { match ty.kind { - ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) => true, + ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Str => true, + ty::Ref(_, ref_ty, _) => is_primitive_type(ref_ty), ty::Array(inner_type, _) | ty::Slice(inner_type) => is_primitive_type(inner_type), ty::Tuple(inner_types) => inner_types.types().all(is_primitive_type), _ => false, diff --git a/tests/ui/stable_sort_primitive.fixed b/tests/ui/stable_sort_primitive.fixed index d864693e3af4..dacd0ccf3b8b 100644 --- a/tests/ui/stable_sort_primitive.fixed +++ b/tests/ui/stable_sort_primitive.fixed @@ -9,6 +9,8 @@ fn main() { vec.sort_unstable(); let mut vec = vec!['a', 'A', 'c']; vec.sort_unstable(); + let mut vec: Vec<&'static str> = vec!["ab", "cd", "ab", "bc"]; + vec.sort_unstable(); let mut arr = [1, 3, 2]; arr.sort_unstable(); // Negative examples: behavior changes if made unstable diff --git a/tests/ui/stable_sort_primitive.rs b/tests/ui/stable_sort_primitive.rs index 1102269886b6..0f1ddc186fbe 100644 --- a/tests/ui/stable_sort_primitive.rs +++ b/tests/ui/stable_sort_primitive.rs @@ -9,6 +9,8 @@ fn main() { vec.sort(); let mut vec = vec!['a', 'A', 'c']; vec.sort(); + let mut vec: Vec<&'static str> = vec!["ab", "cd", "ab", "bc"]; + vec.sort(); let mut arr = [1, 3, 2]; arr.sort(); // Negative examples: behavior changes if made unstable diff --git a/tests/ui/stable_sort_primitive.stderr b/tests/ui/stable_sort_primitive.stderr index e04092360fef..f48a74c6dfa7 100644 --- a/tests/ui/stable_sort_primitive.stderr +++ b/tests/ui/stable_sort_primitive.stderr @@ -21,8 +21,14 @@ LL | vec.sort(); error: Use sort_unstable instead of sort --> $DIR/stable_sort_primitive.rs:13:5 | +LL | vec.sort(); + | ^^^^^^^^^^ help: try: `vec.sort_unstable()` + +error: Use sort_unstable instead of sort + --> $DIR/stable_sort_primitive.rs:15:5 + | LL | arr.sort(); | ^^^^^^^^^^ help: try: `arr.sort_unstable()` -error: aborting due to 4 previous errors +error: aborting due to 5 previous errors