2021-03-12 09:42:43 -06:00
|
|
|
// run-rustfix
|
|
|
|
#![warn(clippy::needless_for_each)]
|
2022-10-02 14:13:22 -05:00
|
|
|
#![allow(unused)]
|
2022-03-17 18:53:28 -05:00
|
|
|
#![allow(
|
2022-10-02 14:13:22 -05:00
|
|
|
clippy::let_unit_value,
|
2022-03-17 18:53:28 -05:00
|
|
|
clippy::match_single_binding,
|
2022-10-02 14:13:22 -05:00
|
|
|
clippy::needless_return,
|
|
|
|
clippy::uninlined_format_args
|
2022-03-17 18:53:28 -05:00
|
|
|
)]
|
2021-03-12 09:42:43 -06:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
fn should_lint() {
|
|
|
|
let v: Vec<i32> = Vec::new();
|
|
|
|
let mut acc = 0;
|
|
|
|
for elem in v.iter() {
|
|
|
|
acc += elem;
|
|
|
|
}
|
|
|
|
for elem in v.into_iter() {
|
|
|
|
acc += elem;
|
|
|
|
}
|
|
|
|
|
2021-03-13 00:11:39 -06:00
|
|
|
for elem in [1, 2, 3].iter() {
|
|
|
|
acc += elem;
|
|
|
|
}
|
|
|
|
|
2021-03-12 09:42:43 -06:00
|
|
|
let mut hash_map: HashMap<i32, i32> = HashMap::new();
|
|
|
|
for (k, v) in hash_map.iter() {
|
|
|
|
acc += k + v;
|
|
|
|
}
|
|
|
|
for (k, v) in hash_map.iter_mut() {
|
|
|
|
acc += *k + *v;
|
|
|
|
}
|
|
|
|
for k in hash_map.keys() {
|
|
|
|
acc += k;
|
|
|
|
}
|
|
|
|
for v in hash_map.values() {
|
|
|
|
acc += v;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn my_vec() -> Vec<i32> {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
for elem in my_vec().iter() {
|
|
|
|
acc += elem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn should_not_lint() {
|
|
|
|
let v: Vec<i32> = Vec::new();
|
|
|
|
let mut acc = 0;
|
|
|
|
|
|
|
|
// `for_each` argument is not closure.
|
|
|
|
fn print(x: &i32) {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
v.iter().for_each(print);
|
|
|
|
|
2021-03-13 00:11:39 -06:00
|
|
|
// User defined type.
|
|
|
|
struct MyStruct {
|
|
|
|
v: Vec<i32>,
|
|
|
|
}
|
|
|
|
impl MyStruct {
|
|
|
|
fn iter(&self) -> impl Iterator<Item = &i32> {
|
|
|
|
self.v.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let s = MyStruct { v: Vec::new() };
|
|
|
|
s.iter().for_each(|elem| {
|
|
|
|
acc += elem;
|
|
|
|
});
|
|
|
|
|
2021-03-12 09:42:43 -06:00
|
|
|
// `for_each` follows long iterator chain.
|
2021-03-13 00:11:39 -06:00
|
|
|
v.iter().chain(v.iter()).for_each(|v| {
|
|
|
|
acc += v;
|
|
|
|
});
|
2021-03-12 09:42:43 -06:00
|
|
|
v.as_slice().iter().for_each(|v| {
|
|
|
|
acc += v;
|
|
|
|
});
|
2021-03-13 00:11:39 -06:00
|
|
|
s.v.iter().for_each(|v| {
|
|
|
|
acc += v;
|
|
|
|
});
|
2021-03-12 09:42:43 -06:00
|
|
|
|
|
|
|
// `return` is used in `Loop` of the closure.
|
|
|
|
v.iter().for_each(|v| {
|
|
|
|
for i in 0..*v {
|
|
|
|
if i == 10 {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
println!("{}", v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *v == 20 {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
println!("{}", v);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Previously transformed iterator variable.
|
|
|
|
let it = v.iter();
|
|
|
|
it.chain(v.iter()).for_each(|elem| {
|
|
|
|
acc += elem;
|
|
|
|
});
|
|
|
|
|
|
|
|
// `for_each` is not directly in a statement.
|
|
|
|
match 1 {
|
|
|
|
_ => v.iter().for_each(|elem| {
|
|
|
|
acc += elem;
|
|
|
|
}),
|
|
|
|
}
|
2021-03-13 19:22:28 -06:00
|
|
|
|
|
|
|
// `for_each` is in a let bingind.
|
|
|
|
let _ = v.iter().for_each(|elem| {
|
|
|
|
acc += elem;
|
|
|
|
});
|
2021-03-12 09:42:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|