2020-05-15 11:20:07 -05:00
|
|
|
#![warn(clippy::for_loops_over_fallibles)]
|
2018-10-30 15:25:34 -05:00
|
|
|
|
2020-05-15 11:20:07 -05:00
|
|
|
fn for_loops_over_fallibles() {
|
2018-10-30 15:25:34 -05:00
|
|
|
let option = Some(1);
|
2022-06-04 04:03:11 -05:00
|
|
|
let mut result = option.ok_or("x not found");
|
2018-10-30 15:25:34 -05:00
|
|
|
let v = vec![0, 1, 2];
|
|
|
|
|
2020-05-03 08:16:00 -05:00
|
|
|
// check over an `Option`
|
2018-10-30 15:25:34 -05:00
|
|
|
for x in option {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
2022-06-04 04:03:11 -05:00
|
|
|
// check over an `Option`
|
|
|
|
for x in option.iter() {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
2020-05-03 08:16:00 -05:00
|
|
|
// check over a `Result`
|
2018-10-30 15:25:34 -05:00
|
|
|
for x in result {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
2022-06-04 04:03:11 -05:00
|
|
|
// check over a `Result`
|
2022-06-05 03:26:29 -05:00
|
|
|
for x in result.iter_mut() {
|
2022-06-04 04:03:11 -05:00
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check over a `Result`
|
2022-06-05 03:26:29 -05:00
|
|
|
for x in result.into_iter() {
|
2022-06-04 04:03:11 -05:00
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
2018-10-30 15:25:34 -05:00
|
|
|
for x in option.ok_or("x not found") {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure LOOP_OVER_NEXT lint takes clippy::precedence when next() is the last call
|
|
|
|
// in the chain
|
|
|
|
for x in v.iter().next() {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we lint when next() is not the last call in the chain
|
|
|
|
for x in v.iter().next().and(Some(0)) {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
for x in v.iter().next().ok_or("x not found") {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for false positives
|
|
|
|
|
|
|
|
// for loop false positive
|
|
|
|
for x in v {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
// while let false positive for Option
|
|
|
|
while let Some(x) = option {
|
|
|
|
println!("{}", x);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// while let false positive for Result
|
|
|
|
while let Ok(x) = result {
|
|
|
|
println!("{}", x);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|