Skip to content

Commit 1b4e2ef

Browse files
committed
fix empty needle corner case and add tests
1 parent 676f1f6 commit 1b4e2ef

File tree

4 files changed

+203
-2
lines changed

4 files changed

+203
-2
lines changed

clippy_lints/src/matches/redundant_guards.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,13 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
127127
{
128128
// `arr if arr.starts_with(&[123])` becomes [123, ..]
129129
// `arr if arr.ends_with(&[123])` becomes [.., 123]
130+
// `arr if arr.starts_with(&[])` becomes [..] (why would anyone write this?)
130131

131132
let mut sugg = snippet(cx, needle.span, "<needle>").into_owned();
132133

133-
if path.ident.name == sym!(starts_with) {
134+
if needles.is_empty() {
135+
sugg.insert_str(1, "..");
136+
} else if path.ident.name == sym!(starts_with) {
134137
sugg.insert_str(sugg.len() - 1, ", ..");
135138
} else if path.ident.name == sym!(ends_with) {
136139
sugg.insert_str(1, ".., ");

tests/ui/redundant_guards.fixed

+57
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,60 @@ mod issue11465 {
193193
}
194194
}
195195
}
196+
197+
fn issue11807() {
198+
#![allow(clippy::single_match)]
199+
200+
match Some(Some("")) {
201+
Some(Some("")) => {},
202+
_ => {},
203+
}
204+
205+
match Some(Some(String::new())) {
206+
// Do not lint: String deref-coerces to &str
207+
Some(Some(x)) if x.is_empty() => {},
208+
_ => {},
209+
}
210+
211+
match Some(Some(&[] as &[i32])) {
212+
Some(Some([])) => {},
213+
_ => {},
214+
}
215+
216+
match Some(Some([] as [i32; 0])) {
217+
Some(Some([])) => {},
218+
_ => {},
219+
}
220+
221+
match Some(Some(Vec::<()>::new())) {
222+
// Do not lint: Vec deref-coerces to &[T]
223+
Some(Some(x)) if x.is_empty() => {},
224+
_ => {},
225+
}
226+
227+
match Some(Some(&[] as &[i32])) {
228+
Some(Some([..])) => {},
229+
_ => {},
230+
}
231+
232+
match Some(Some(&[] as &[i32])) {
233+
Some(Some([1, ..])) => {},
234+
_ => {},
235+
}
236+
237+
match Some(Some(&[] as &[i32])) {
238+
Some(Some([1, 2, ..])) => {},
239+
_ => {},
240+
}
241+
242+
match Some(Some(&[] as &[i32])) {
243+
Some(Some([.., 1, 2])) => {},
244+
_ => {},
245+
}
246+
247+
match Some(Some(Vec::<i32>::new())) {
248+
// Do not lint: deref coercion
249+
Some(Some(x)) if x.starts_with(&[1, 2]) => {},
250+
_ => {},
251+
}
252+
}

tests/ui/redundant_guards.rs

+57
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,60 @@ mod issue11465 {
193193
}
194194
}
195195
}
196+
197+
fn issue11807() {
198+
#![allow(clippy::single_match)]
199+
200+
match Some(Some("")) {
201+
Some(Some(x)) if x.is_empty() => {},
202+
_ => {},
203+
}
204+
205+
match Some(Some(String::new())) {
206+
// Do not lint: String deref-coerces to &str
207+
Some(Some(x)) if x.is_empty() => {},
208+
_ => {},
209+
}
210+
211+
match Some(Some(&[] as &[i32])) {
212+
Some(Some(x)) if x.is_empty() => {},
213+
_ => {},
214+
}
215+
216+
match Some(Some([] as [i32; 0])) {
217+
Some(Some(x)) if x.is_empty() => {},
218+
_ => {},
219+
}
220+
221+
match Some(Some(Vec::<()>::new())) {
222+
// Do not lint: Vec deref-coerces to &[T]
223+
Some(Some(x)) if x.is_empty() => {},
224+
_ => {},
225+
}
226+
227+
match Some(Some(&[] as &[i32])) {
228+
Some(Some(x)) if x.starts_with(&[]) => {},
229+
_ => {},
230+
}
231+
232+
match Some(Some(&[] as &[i32])) {
233+
Some(Some(x)) if x.starts_with(&[1]) => {},
234+
_ => {},
235+
}
236+
237+
match Some(Some(&[] as &[i32])) {
238+
Some(Some(x)) if x.starts_with(&[1, 2]) => {},
239+
_ => {},
240+
}
241+
242+
match Some(Some(&[] as &[i32])) {
243+
Some(Some(x)) if x.ends_with(&[1, 2]) => {},
244+
_ => {},
245+
}
246+
247+
match Some(Some(Vec::<i32>::new())) {
248+
// Do not lint: deref coercion
249+
Some(Some(x)) if x.starts_with(&[1, 2]) => {},
250+
_ => {},
251+
}
252+
}

tests/ui/redundant_guards.stderr

+85-1
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,89 @@ LL - B { ref c, .. } if matches!(c, &1) => {},
203203
LL + B { c: 1, .. } => {},
204204
|
205205

206-
error: aborting due to 17 previous errors
206+
error: redundant guard
207+
--> $DIR/redundant_guards.rs:201:26
208+
|
209+
LL | Some(Some(x)) if x.is_empty() => {},
210+
| ^^^^^^^^^^^^
211+
|
212+
help: try
213+
|
214+
LL - Some(Some(x)) if x.is_empty() => {},
215+
LL + Some(Some("")) => {},
216+
|
217+
218+
error: redundant guard
219+
--> $DIR/redundant_guards.rs:212:26
220+
|
221+
LL | Some(Some(x)) if x.is_empty() => {},
222+
| ^^^^^^^^^^^^
223+
|
224+
help: try
225+
|
226+
LL - Some(Some(x)) if x.is_empty() => {},
227+
LL + Some(Some([])) => {},
228+
|
229+
230+
error: redundant guard
231+
--> $DIR/redundant_guards.rs:217:26
232+
|
233+
LL | Some(Some(x)) if x.is_empty() => {},
234+
| ^^^^^^^^^^^^
235+
|
236+
help: try
237+
|
238+
LL - Some(Some(x)) if x.is_empty() => {},
239+
LL + Some(Some([])) => {},
240+
|
241+
242+
error: redundant guard
243+
--> $DIR/redundant_guards.rs:228:26
244+
|
245+
LL | Some(Some(x)) if x.starts_with(&[]) => {},
246+
| ^^^^^^^^^^^^^^^^^^
247+
|
248+
help: try
249+
|
250+
LL - Some(Some(x)) if x.starts_with(&[]) => {},
251+
LL + Some(Some([..])) => {},
252+
|
253+
254+
error: redundant guard
255+
--> $DIR/redundant_guards.rs:233:26
256+
|
257+
LL | Some(Some(x)) if x.starts_with(&[1]) => {},
258+
| ^^^^^^^^^^^^^^^^^^^
259+
|
260+
help: try
261+
|
262+
LL - Some(Some(x)) if x.starts_with(&[1]) => {},
263+
LL + Some(Some([1, ..])) => {},
264+
|
265+
266+
error: redundant guard
267+
--> $DIR/redundant_guards.rs:238:26
268+
|
269+
LL | Some(Some(x)) if x.starts_with(&[1, 2]) => {},
270+
| ^^^^^^^^^^^^^^^^^^^^^^
271+
|
272+
help: try
273+
|
274+
LL - Some(Some(x)) if x.starts_with(&[1, 2]) => {},
275+
LL + Some(Some([1, 2, ..])) => {},
276+
|
277+
278+
error: redundant guard
279+
--> $DIR/redundant_guards.rs:243:26
280+
|
281+
LL | Some(Some(x)) if x.ends_with(&[1, 2]) => {},
282+
| ^^^^^^^^^^^^^^^^^^^^
283+
|
284+
help: try
285+
|
286+
LL - Some(Some(x)) if x.ends_with(&[1, 2]) => {},
287+
LL + Some(Some([.., 1, 2])) => {},
288+
|
289+
290+
error: aborting due to 24 previous errors
207291

0 commit comments

Comments
 (0)