Skip to content

Commit

Permalink
Also lint for static strings
Browse files Browse the repository at this point in the history
  • Loading branch information
JarredAllen committed Aug 3, 2020
1 parent 7afce17 commit 3d0628d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/stable_sort_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/stable_sort_primitive.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/stable_sort_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/stable_sort_primitive.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3d0628d

Please sign in to comment.