2022-09-21 12:02:37 -05:00
|
|
|
#![warn(clippy::iter_kv_map)]
|
2023-01-12 12:48:13 -06:00
|
|
|
#![allow(unused_mut, clippy::redundant_clone, clippy::suspicious_map, clippy::map_identity)]
|
2022-09-21 12:02:37 -05:00
|
|
|
|
|
|
|
use std::collections::{BTreeMap, HashMap};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let get_key = |(key, _val)| key;
|
2023-01-12 12:48:13 -06:00
|
|
|
fn ref_acceptor(v: &u32) -> u32 {
|
|
|
|
*v
|
|
|
|
}
|
2022-09-21 12:02:37 -05:00
|
|
|
|
|
|
|
let map: HashMap<u32, u32> = HashMap::new();
|
|
|
|
|
|
|
|
let _ = map.iter().map(|(key, _)| key).collect::<Vec<_>>();
|
|
|
|
let _ = map.iter().map(|(_, value)| value).collect::<Vec<_>>();
|
|
|
|
let _ = map.iter().map(|(_, v)| v + 2).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let _ = map.clone().into_iter().map(|(key, _)| key).collect::<Vec<_>>();
|
|
|
|
let _ = map.clone().into_iter().map(|(key, _)| key + 2).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let _ = map.clone().into_iter().map(|(_, val)| val).collect::<Vec<_>>();
|
|
|
|
let _ = map.clone().into_iter().map(|(_, val)| val + 2).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let _ = map.clone().iter().map(|(_, val)| val).collect::<Vec<_>>();
|
|
|
|
let _ = map.iter().map(|(key, _)| key).filter(|x| *x % 2 == 0).count();
|
|
|
|
|
|
|
|
// Don't lint
|
|
|
|
let _ = map.iter().filter(|(_, val)| *val % 2 == 0).map(|(key, _)| key).count();
|
|
|
|
let _ = map.iter().map(get_key).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// Linting the following could be an improvement to the lint
|
|
|
|
// map.iter().filter_map(|(_, val)| (val % 2 == 0).then(val * 17)).count();
|
|
|
|
|
|
|
|
// Lint
|
|
|
|
let _ = map.iter().map(|(key, _value)| key * 9).count();
|
|
|
|
let _ = map.iter().map(|(_key, value)| value * 17).count();
|
|
|
|
|
2023-01-12 12:48:13 -06:00
|
|
|
// Preserve the ref in the fix.
|
|
|
|
let _ = map.clone().into_iter().map(|(_, ref val)| ref_acceptor(val)).count();
|
|
|
|
|
|
|
|
// Preserve the mut in the fix.
|
|
|
|
let _ = map
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, mut val)| {
|
|
|
|
val += 2;
|
|
|
|
val
|
|
|
|
})
|
|
|
|
.count();
|
|
|
|
|
|
|
|
// Don't let a mut interfere.
|
|
|
|
let _ = map.clone().into_iter().map(|(_, mut val)| val).count();
|
|
|
|
|
2022-09-21 12:02:37 -05:00
|
|
|
let map: BTreeMap<u32, u32> = BTreeMap::new();
|
|
|
|
|
|
|
|
let _ = map.iter().map(|(key, _)| key).collect::<Vec<_>>();
|
|
|
|
let _ = map.iter().map(|(_, value)| value).collect::<Vec<_>>();
|
|
|
|
let _ = map.iter().map(|(_, v)| v + 2).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let _ = map.clone().into_iter().map(|(key, _)| key).collect::<Vec<_>>();
|
|
|
|
let _ = map.clone().into_iter().map(|(key, _)| key + 2).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let _ = map.clone().into_iter().map(|(_, val)| val).collect::<Vec<_>>();
|
|
|
|
let _ = map.clone().into_iter().map(|(_, val)| val + 2).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let _ = map.clone().iter().map(|(_, val)| val).collect::<Vec<_>>();
|
|
|
|
let _ = map.iter().map(|(key, _)| key).filter(|x| *x % 2 == 0).count();
|
|
|
|
|
|
|
|
// Don't lint
|
|
|
|
let _ = map.iter().filter(|(_, val)| *val % 2 == 0).map(|(key, _)| key).count();
|
|
|
|
let _ = map.iter().map(get_key).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// Linting the following could be an improvement to the lint
|
|
|
|
// map.iter().filter_map(|(_, val)| (val % 2 == 0).then(val * 17)).count();
|
|
|
|
|
|
|
|
// Lint
|
|
|
|
let _ = map.iter().map(|(key, _value)| key * 9).count();
|
|
|
|
let _ = map.iter().map(|(_key, value)| value * 17).count();
|
2023-01-12 12:48:13 -06:00
|
|
|
|
|
|
|
// Preserve the ref in the fix.
|
|
|
|
let _ = map.clone().into_iter().map(|(_, ref val)| ref_acceptor(val)).count();
|
|
|
|
|
|
|
|
// Preserve the mut in the fix.
|
|
|
|
let _ = map
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, mut val)| {
|
|
|
|
val += 2;
|
|
|
|
val
|
|
|
|
})
|
|
|
|
.count();
|
|
|
|
|
|
|
|
// Don't let a mut interfere.
|
|
|
|
let _ = map.clone().into_iter().map(|(_, mut val)| val).count();
|
2022-09-21 12:02:37 -05:00
|
|
|
}
|
2023-12-01 11:21:58 -06:00
|
|
|
|
|
|
|
#[clippy::msrv = "1.53"]
|
|
|
|
fn msrv_1_53() {
|
|
|
|
let map: HashMap<u32, u32> = HashMap::new();
|
|
|
|
|
|
|
|
// Don't lint because into_iter is not supported
|
|
|
|
let _ = map.clone().into_iter().map(|(key, _)| key).collect::<Vec<_>>();
|
|
|
|
let _ = map.clone().into_iter().map(|(key, _)| key + 2).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let _ = map.clone().into_iter().map(|(_, val)| val).collect::<Vec<_>>();
|
|
|
|
let _ = map.clone().into_iter().map(|(_, val)| val + 2).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// Lint
|
|
|
|
let _ = map.iter().map(|(key, _)| key).collect::<Vec<_>>();
|
|
|
|
//~^ ERROR: iterating on a map's keys
|
|
|
|
let _ = map.iter().map(|(_, value)| value).collect::<Vec<_>>();
|
|
|
|
//~^ ERROR: iterating on a map's values
|
|
|
|
let _ = map.iter().map(|(_, v)| v + 2).collect::<Vec<_>>();
|
|
|
|
//~^ ERROR: iterating on a map's values
|
|
|
|
}
|
|
|
|
|
|
|
|
#[clippy::msrv = "1.54"]
|
|
|
|
fn msrv_1_54() {
|
|
|
|
// Lint all
|
|
|
|
let map: HashMap<u32, u32> = HashMap::new();
|
|
|
|
|
|
|
|
let _ = map.clone().into_iter().map(|(key, _)| key).collect::<Vec<_>>();
|
|
|
|
//~^ ERROR: iterating on a map's keys
|
|
|
|
let _ = map.clone().into_iter().map(|(key, _)| key + 2).collect::<Vec<_>>();
|
|
|
|
//~^ ERROR: iterating on a map's keys
|
|
|
|
|
|
|
|
let _ = map.clone().into_iter().map(|(_, val)| val).collect::<Vec<_>>();
|
|
|
|
//~^ ERROR: iterating on a map's values
|
|
|
|
let _ = map.clone().into_iter().map(|(_, val)| val + 2).collect::<Vec<_>>();
|
|
|
|
//~^ ERROR: iterating on a map's values
|
|
|
|
|
|
|
|
let _ = map.iter().map(|(key, _)| key).collect::<Vec<_>>();
|
|
|
|
//~^ ERROR: iterating on a map's keys
|
|
|
|
let _ = map.iter().map(|(_, value)| value).collect::<Vec<_>>();
|
|
|
|
//~^ ERROR: iterating on a map's values
|
|
|
|
let _ = map.iter().map(|(_, v)| v + 2).collect::<Vec<_>>();
|
|
|
|
//~^ ERROR: iterating on a map's values
|
|
|
|
}
|