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`.
36 lines
875 B
Rust
36 lines
875 B
Rust
#![allow(clippy::map_with_unused_argument_over_ranges)]
|
|
#![warn(clippy::suspicious_map)]
|
|
|
|
fn main() {
|
|
let _ = (0..3).map(|x| x + 2).count();
|
|
//~^ ERROR: this call to `map()` won't have an effect on the call to `count()`
|
|
|
|
let f = |x| x + 1;
|
|
let _ = (0..3).map(f).count();
|
|
//~^ ERROR: this call to `map()` won't have an effect on the call to `count()`
|
|
}
|
|
|
|
fn negative() {
|
|
// closure with side effects
|
|
let mut sum = 0;
|
|
let _ = (0..3).map(|x| sum += x).count();
|
|
|
|
// closure variable with side effects
|
|
let ext_closure = |x| sum += x;
|
|
let _ = (0..3).map(ext_closure).count();
|
|
|
|
// closure that returns unit
|
|
let _ = (0..3)
|
|
.map(|x| {
|
|
// do nothing
|
|
})
|
|
.count();
|
|
|
|
// external function
|
|
let _ = (0..3).map(do_something).count();
|
|
}
|
|
|
|
fn do_something<T>(t: T) -> String {
|
|
unimplemented!()
|
|
}
|