fix empty needle corner case and add tests

This commit is contained in:
y21 2023-11-15 21:10:03 +01:00
parent 676f1f6ef8
commit 1b4e2ef3d7
4 changed files with 203 additions and 2 deletions

View File

@ -127,10 +127,13 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
{
// `arr if arr.starts_with(&[123])` becomes [123, ..]
// `arr if arr.ends_with(&[123])` becomes [.., 123]
// `arr if arr.starts_with(&[])` becomes [..] (why would anyone write this?)
let mut sugg = snippet(cx, needle.span, "<needle>").into_owned();
if path.ident.name == sym!(starts_with) {
if needles.is_empty() {
sugg.insert_str(1, "..");
} else if path.ident.name == sym!(starts_with) {
sugg.insert_str(sugg.len() - 1, ", ..");
} else if path.ident.name == sym!(ends_with) {
sugg.insert_str(1, ".., ");

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 @@ fn 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