Auto merge of #11818 - y21:more_redundant_guards, r=llogiq

[`redundant_guards`]: catch `is_empty`, `starts_with` and `ends_with` on slices and `str`s

Fixes #11807

Few things worth mentioning:
- Taking `snippet`s is now done at callsite, instead of passing a span and doing it in `emit_redundant_guards`. This is because we now need custom suggestion strings in certain places, like `""` for `str::is_empty`.
- This now uses `snippet` instead of `snippet_with_applicability`. I don't think this really makes any difference for `MaybeIncorrect`, though?
- This could also lint byte strings, as they're of type `&[u8; N]`, but that can be ugly so I decided to leave it out for now

changelog: [`redundant_guards`]: catch `str::is_empty`, `slice::is_empty`, `slice::starts_with` and `slice::ends_with`
This commit is contained in:
bors 2023-11-29 13:20:59 +00:00
commit 8b0bf6423d
5 changed files with 293 additions and 24 deletions

View file

@ -193,3 +193,60 @@ mod issue11465 {
}
}
}
fn issue11807() {
#![allow(clippy::single_match)]
match Some(Some("")) {
Some(Some("")) => {},
_ => {},
}
match Some(Some(String::new())) {
// Do not lint: String deref-coerces to &str
Some(Some(x)) if x.is_empty() => {},
_ => {},
}
match Some(Some(&[] as &[i32])) {
Some(Some([])) => {},
_ => {},
}
match Some(Some([] as [i32; 0])) {
Some(Some([])) => {},
_ => {},
}
match Some(Some(Vec::<()>::new())) {
// Do not lint: Vec deref-coerces to &[T]
Some(Some(x)) if x.is_empty() => {},
_ => {},
}
match Some(Some(&[] as &[i32])) {
Some(Some([..])) => {},
_ => {},
}
match Some(Some(&[] as &[i32])) {
Some(Some([1, ..])) => {},
_ => {},
}
match Some(Some(&[] as &[i32])) {
Some(Some([1, 2, ..])) => {},
_ => {},
}
match Some(Some(&[] as &[i32])) {
Some(Some([.., 1, 2])) => {},
_ => {},
}
match Some(Some(Vec::<i32>::new())) {
// Do not lint: deref coercion
Some(Some(x)) if x.starts_with(&[1, 2]) => {},
_ => {},
}
}

View file

@ -193,3 +193,60 @@ mod issue11465 {
}
}
}
fn issue11807() {
#![allow(clippy::single_match)]
match Some(Some("")) {
Some(Some(x)) if x.is_empty() => {},
_ => {},
}
match Some(Some(String::new())) {
// Do not lint: String deref-coerces to &str
Some(Some(x)) if x.is_empty() => {},
_ => {},
}
match Some(Some(&[] as &[i32])) {
Some(Some(x)) if x.is_empty() => {},
_ => {},
}
match Some(Some([] as [i32; 0])) {
Some(Some(x)) if x.is_empty() => {},
_ => {},
}
match Some(Some(Vec::<()>::new())) {
// Do not lint: Vec deref-coerces to &[T]
Some(Some(x)) if x.is_empty() => {},
_ => {},
}
match Some(Some(&[] as &[i32])) {
Some(Some(x)) if x.starts_with(&[]) => {},
_ => {},
}
match Some(Some(&[] as &[i32])) {
Some(Some(x)) if x.starts_with(&[1]) => {},
_ => {},
}
match Some(Some(&[] as &[i32])) {
Some(Some(x)) if x.starts_with(&[1, 2]) => {},
_ => {},
}
match Some(Some(&[] as &[i32])) {
Some(Some(x)) if x.ends_with(&[1, 2]) => {},
_ => {},
}
match Some(Some(Vec::<i32>::new())) {
// Do not lint: deref coercion
Some(Some(x)) if x.starts_with(&[1, 2]) => {},
_ => {},
}
}

View file

@ -203,5 +203,89 @@ LL - B { ref c, .. } if matches!(c, &1) => {},
LL + B { c: 1, .. } => {},
|
error: aborting due to 17 previous errors
error: redundant guard
--> $DIR/redundant_guards.rs:201:26
|
LL | Some(Some(x)) if x.is_empty() => {},
| ^^^^^^^^^^^^
|
help: try
|
LL - Some(Some(x)) if x.is_empty() => {},
LL + Some(Some("")) => {},
|
error: redundant guard
--> $DIR/redundant_guards.rs:212:26
|
LL | Some(Some(x)) if x.is_empty() => {},
| ^^^^^^^^^^^^
|
help: try
|
LL - Some(Some(x)) if x.is_empty() => {},
LL + Some(Some([])) => {},
|
error: redundant guard
--> $DIR/redundant_guards.rs:217:26
|
LL | Some(Some(x)) if x.is_empty() => {},
| ^^^^^^^^^^^^
|
help: try
|
LL - Some(Some(x)) if x.is_empty() => {},
LL + Some(Some([])) => {},
|
error: redundant guard
--> $DIR/redundant_guards.rs:228:26
|
LL | Some(Some(x)) if x.starts_with(&[]) => {},
| ^^^^^^^^^^^^^^^^^^
|
help: try
|
LL - Some(Some(x)) if x.starts_with(&[]) => {},
LL + Some(Some([..])) => {},
|
error: redundant guard
--> $DIR/redundant_guards.rs:233:26
|
LL | Some(Some(x)) if x.starts_with(&[1]) => {},
| ^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL - Some(Some(x)) if x.starts_with(&[1]) => {},
LL + Some(Some([1, ..])) => {},
|
error: redundant guard
--> $DIR/redundant_guards.rs:238:26
|
LL | Some(Some(x)) if x.starts_with(&[1, 2]) => {},
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL - Some(Some(x)) if x.starts_with(&[1, 2]) => {},
LL + Some(Some([1, 2, ..])) => {},
|
error: redundant guard
--> $DIR/redundant_guards.rs:243:26
|
LL | Some(Some(x)) if x.ends_with(&[1, 2]) => {},
| ^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL - Some(Some(x)) if x.ends_with(&[1, 2]) => {},
LL + Some(Some([.., 1, 2])) => {},
|
error: aborting due to 24 previous errors