acc3842d43
This lint checks for code that looks like ```rust let something : Vec<_> = (0..100).map(|_| { 1 + 2 + 3 }).collect(); ``` which is more clear as ```rust let something : Vec<_> = std::iter::repeat_with(|| { 1 + 2 + 3 }).take(100).collect(); ``` or ```rust let something : Vec<_> = std::iter::repeat_n(1 + 2 + 3, 100) .collect(); ``` That is, a map over a range which does nothing with the parameter passed to it is simply a function (or closure) being called `n` times and could be more semantically expressed using `take`.
74 lines
2.4 KiB
Rust
74 lines
2.4 KiB
Rust
#![allow(
|
|
unused,
|
|
clippy::redundant_closure,
|
|
clippy::reversed_empty_ranges,
|
|
clippy::identity_op
|
|
)]
|
|
#![warn(clippy::map_with_unused_argument_over_ranges)]
|
|
|
|
fn do_something() -> usize {
|
|
todo!()
|
|
}
|
|
|
|
fn do_something_interesting(x: usize, y: usize) -> usize {
|
|
todo!()
|
|
}
|
|
|
|
macro_rules! gen {
|
|
() => {
|
|
(0..10).map(|_| do_something());
|
|
};
|
|
}
|
|
|
|
fn main() {
|
|
// These should be raised
|
|
std::iter::repeat_with(|| do_something()).take(10);
|
|
std::iter::repeat_with(|| do_something()).take(10);
|
|
std::iter::repeat_with(|| do_something()).take(11);
|
|
std::iter::repeat_with(|| do_something()).take(7);
|
|
std::iter::repeat_with(|| do_something()).take(8);
|
|
std::iter::repeat_n(3, 10);
|
|
std::iter::repeat_with(|| {
|
|
let x = 3;
|
|
x + 2
|
|
}).take(10);
|
|
std::iter::repeat_with(|| do_something()).take(10).collect::<Vec<_>>();
|
|
let upper = 4;
|
|
std::iter::repeat_with(|| do_something()).take(upper);
|
|
let upper_fn = || 4;
|
|
std::iter::repeat_with(|| do_something()).take(upper_fn());
|
|
std::iter::repeat_with(|| do_something()).take(upper_fn() + 1);
|
|
std::iter::repeat_with(|| do_something()).take(upper_fn() - 2);
|
|
std::iter::repeat_with(|| do_something()).take(upper_fn() - 1);
|
|
(-3..9).map(|_| do_something());
|
|
std::iter::repeat_with(|| do_something()).take(0);
|
|
std::iter::repeat_with(|| do_something()).take(1);
|
|
std::iter::repeat_with(|| do_something()).take((1 << 4) - 0);
|
|
// These should not be raised
|
|
gen!();
|
|
let lower = 2;
|
|
let lower_fn = || 2;
|
|
(lower..upper_fn()).map(|_| do_something()); // Ranges not starting at zero not yet handled
|
|
(lower_fn()..upper_fn()).map(|_| do_something()); // Ranges not starting at zero not yet handled
|
|
(lower_fn()..=upper_fn()).map(|_| do_something()); // Ranges not starting at zero not yet handled
|
|
(0..10).map(|x| do_something_interesting(x, 4)); // Actual map over range
|
|
"Foobar".chars().map(|_| do_something()); // Not a map over range
|
|
// i128::MAX == 340282366920938463463374607431768211455
|
|
(0..=340282366920938463463374607431768211455).map(|_: u128| do_something()); // Can't be replaced due to overflow
|
|
}
|
|
|
|
#[clippy::msrv = "1.27"]
|
|
fn msrv_1_27() {
|
|
(0..10).map(|_| do_something());
|
|
}
|
|
|
|
#[clippy::msrv = "1.28"]
|
|
fn msrv_1_28() {
|
|
std::iter::repeat_with(|| do_something()).take(10);
|
|
}
|
|
|
|
#[clippy::msrv = "1.81"]
|
|
fn msrv_1_82() {
|
|
std::iter::repeat(3).take(10);
|
|
}
|