rust/tests/ui/map_with_unused_argument_over_ranges.rs
Robert Spencer acc3842d43 Add new map_with_unused_argument_over_ranges lint
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`.
2024-10-29 21:32:00 +00:00

74 lines
2.1 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
(0..10).map(|_| do_something());
(0..10).map(|_foo| do_something());
(0..=10).map(|_| do_something());
(3..10).map(|_| do_something());
(3..=10).map(|_| do_something());
(0..10).map(|_| 3);
(0..10).map(|_| {
let x = 3;
x + 2
});
(0..10).map(|_| do_something()).collect::<Vec<_>>();
let upper = 4;
(0..upper).map(|_| do_something());
let upper_fn = || 4;
(0..upper_fn()).map(|_| do_something());
(0..=upper_fn()).map(|_| do_something());
(2..upper_fn()).map(|_| do_something());
(2..=upper_fn()).map(|_| do_something());
(-3..9).map(|_| do_something());
(9..3).map(|_| do_something());
(9..=9).map(|_| do_something());
(1..=1 << 4).map(|_| do_something());
// 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() {
(0..10).map(|_| do_something());
}
#[clippy::msrv = "1.81"]
fn msrv_1_82() {
(0..10).map(|_| 3);
}