2021-02-11 08:04:38 -06:00
|
|
|
#![warn(clippy::manual_flatten)]
|
2022-10-06 02:44:38 -05:00
|
|
|
#![allow(clippy::useless_vec, clippy::uninlined_format_args)]
|
2023-08-24 14:32:12 -05:00
|
|
|
//@no-rustfix
|
2021-02-11 08:04:38 -06:00
|
|
|
fn main() {
|
|
|
|
// Test for loop over implicitly adjusted `Iterator` with `if let` expression
|
|
|
|
let x = vec![Some(1), Some(2), Some(3)];
|
|
|
|
for n in x {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: unnecessary `if let` since only the `Some` variant of the iterator element
|
2021-02-11 08:04:38 -06:00
|
|
|
if let Some(y) = n {
|
|
|
|
println!("{}", y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 13:34:47 -06:00
|
|
|
// Test for loop over implicitly adjusted `Iterator` with `if let` statement
|
2021-02-11 08:04:38 -06:00
|
|
|
let y: Vec<Result<i32, i32>> = vec![];
|
|
|
|
for n in y.clone() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: unnecessary `if let` since only the `Ok` variant of the iterator element i
|
2021-02-11 08:04:38 -06:00
|
|
|
if let Ok(n) = n {
|
|
|
|
println!("{}", n);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for loop over by reference
|
|
|
|
for n in &y {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: unnecessary `if let` since only the `Ok` variant of the iterator element i
|
2021-02-11 08:04:38 -06:00
|
|
|
if let Ok(n) = n {
|
|
|
|
println!("{}", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for loop over an implicit reference
|
|
|
|
let z = &y;
|
|
|
|
for n in z {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: unnecessary `if let` since only the `Ok` variant of the iterator element i
|
2021-02-11 08:04:38 -06:00
|
|
|
if let Ok(n) = n {
|
|
|
|
println!("{}", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for loop over `Iterator` with `if let` expression
|
|
|
|
let z = vec![Some(1), Some(2), Some(3)];
|
|
|
|
let z = z.iter();
|
|
|
|
for n in z {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: unnecessary `if let` since only the `Some` variant of the iterator element
|
2021-02-11 08:04:38 -06:00
|
|
|
if let Some(m) = n {
|
|
|
|
println!("{}", m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using the `None` variant should not trigger the lint
|
|
|
|
// Note: for an autofixable suggestion, the binding in the for loop has to take the
|
|
|
|
// name of the binding in the `if let`
|
|
|
|
let z = vec![Some(1), Some(2), Some(3)];
|
|
|
|
for n in z {
|
|
|
|
if n.is_none() {
|
|
|
|
println!("Nada.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using the `Err` variant should not trigger the lint
|
|
|
|
for n in y.clone() {
|
|
|
|
if let Err(e) = n {
|
|
|
|
println!("Oops: {}!", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Having an else clause should not trigger the lint
|
|
|
|
for n in y.clone() {
|
|
|
|
if let Ok(n) = n {
|
|
|
|
println!("{}", n);
|
|
|
|
} else {
|
|
|
|
println!("Oops!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
let vec_of_ref = vec![&Some(1)];
|
|
|
|
for n in &vec_of_ref {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: unnecessary `if let` since only the `Some` variant of the iterator element
|
2021-03-25 13:29:11 -05:00
|
|
|
if let Some(n) = n {
|
|
|
|
println!("{:?}", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let vec_of_ref = &vec_of_ref;
|
|
|
|
for n in vec_of_ref {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: unnecessary `if let` since only the `Some` variant of the iterator element
|
2021-03-25 13:29:11 -05:00
|
|
|
if let Some(n) = n {
|
|
|
|
println!("{:?}", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let slice_of_ref = &[&Some(1)];
|
|
|
|
for n in slice_of_ref {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: unnecessary `if let` since only the `Some` variant of the iterator element
|
2021-03-25 13:29:11 -05:00
|
|
|
if let Some(n) = n {
|
|
|
|
println!("{:?}", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:31:47 -05:00
|
|
|
struct Test {
|
|
|
|
a: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut vec_of_struct = [Some(Test { a: 1 }), None];
|
|
|
|
|
|
|
|
// Usage of `if let` expression should not trigger lint
|
|
|
|
for n in vec_of_struct.iter_mut() {
|
|
|
|
if let Some(z) = n {
|
|
|
|
*n = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 08:04:38 -06:00
|
|
|
// Using manual flatten should not trigger the lint
|
|
|
|
for n in vec![Some(1), Some(2), Some(3)].iter().flatten() {
|
|
|
|
println!("{}", n);
|
|
|
|
}
|
2022-07-18 02:39:37 -05:00
|
|
|
|
|
|
|
run_unformatted_tests();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
fn run_unformatted_tests() {
|
|
|
|
// Skip rustfmt here on purpose so the suggestion does not fit in one line
|
|
|
|
for n in vec![
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: unnecessary `if let` since only the `Some` variant of the iterator element
|
2022-07-18 02:39:37 -05:00
|
|
|
Some(1),
|
|
|
|
Some(2),
|
|
|
|
Some(3)
|
|
|
|
].iter() {
|
|
|
|
if let Some(n) = n {
|
|
|
|
println!("{:?}", n);
|
|
|
|
}
|
|
|
|
}
|
2021-02-11 08:04:38 -06:00
|
|
|
}
|