Implement manual flatten lint
This commit is contained in:
parent
8973f2c03a
commit
b87e189694
13 changed files with 208 additions and 106 deletions
|
|
@ -1,31 +0,0 @@
|
|||
#![warn(clippy::for_loops_over_options_or_results)]
|
||||
|
||||
fn main() {
|
||||
let x = vec![Some(1), Some(2), Some(3)];
|
||||
for n in x {
|
||||
if let Some(n) = n {
|
||||
println!("{}", n);
|
||||
}
|
||||
}
|
||||
|
||||
let y: Vec<Result<i32, i32>> = vec![];
|
||||
for n in y.clone() {
|
||||
if let Ok(n) = n {
|
||||
println!("{}", n);
|
||||
}
|
||||
}
|
||||
|
||||
// This should not trigger the lint
|
||||
for n in y.clone() {
|
||||
if let Ok(n) = n {
|
||||
println!("{}", n);
|
||||
} else {
|
||||
println!("Oops!");
|
||||
}
|
||||
}
|
||||
|
||||
// This should not trigger the lint
|
||||
for n in vec![Some(1), Some(2), Some(3)].iter().flatten() {
|
||||
println!("{}", n);
|
||||
}
|
||||
}
|
||||
54
tests/ui/manual_flatten.rs
Normal file
54
tests/ui/manual_flatten.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#![warn(clippy::manual_flatten)]
|
||||
|
||||
fn main() {
|
||||
let x = vec![Some(1), Some(2), Some(3)];
|
||||
for n in x {
|
||||
if let Some(n) = n {
|
||||
println!("{}", n);
|
||||
}
|
||||
}
|
||||
|
||||
let y: Vec<Result<i32, i32>> = vec![];
|
||||
for n in y.clone() {
|
||||
if let Ok(n) = n {
|
||||
println!("{}", n);
|
||||
}
|
||||
}
|
||||
|
||||
let z = vec![Some(1), Some(2), Some(3)];
|
||||
let z = z.iter();
|
||||
for n in z {
|
||||
if let Some(n) = n {
|
||||
println!("{}", n);
|
||||
}
|
||||
}
|
||||
|
||||
// Using the `None` variant should not trigger the lint
|
||||
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!");
|
||||
}
|
||||
}
|
||||
|
||||
// Using manual flatten should not trigger the lint
|
||||
for n in vec![Some(1), Some(2), Some(3)].iter().flatten() {
|
||||
println!("{}", n);
|
||||
}
|
||||
}
|
||||
51
tests/ui/manual_flatten.stderr
Normal file
51
tests/ui/manual_flatten.stderr
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
error: Unnecessary `if let` since only the `Some` variant of the iterator element is used.
|
||||
--> $DIR/manual_flatten.rs:5:5
|
||||
|
|
||||
LL | / for n in x {
|
||||
LL | | if let Some(n) = n {
|
||||
LL | | println!("{}", n);
|
||||
LL | | }
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
|
||||
= note: `-D clippy::manual-flatten` implied by `-D warnings`
|
||||
help: try: `x.into_iter().flatten()`
|
||||
--> $DIR/manual_flatten.rs:5:14
|
||||
|
|
||||
LL | for n in x {
|
||||
| ^
|
||||
|
||||
error: Unnecessary `if let` since only the `Ok` variant of the iterator element is used.
|
||||
--> $DIR/manual_flatten.rs:12:5
|
||||
|
|
||||
LL | / for n in y.clone() {
|
||||
LL | | if let Ok(n) = n {
|
||||
LL | | println!("{}", n);
|
||||
LL | | }
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
|
||||
help: try: `y.clone().into_iter().flatten()`
|
||||
--> $DIR/manual_flatten.rs:12:14
|
||||
|
|
||||
LL | for n in y.clone() {
|
||||
| ^^^^^^^^^
|
||||
|
||||
error: Unnecessary `if let` since only the `Some` variant of the iterator element is used.
|
||||
--> $DIR/manual_flatten.rs:20:5
|
||||
|
|
||||
LL | / for n in z {
|
||||
LL | | if let Some(n) = n {
|
||||
LL | | println!("{}", n);
|
||||
LL | | }
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
|
||||
help: try: `z.flatten()`
|
||||
--> $DIR/manual_flatten.rs:20:14
|
||||
|
|
||||
LL | for n in z {
|
||||
| ^
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue