rust-clippy/tests/ui/for_kv_map.fixed

67 lines
1.8 KiB
Rust
Raw Normal View History

2023-07-27 11:40:22 +00:00
#![warn(clippy::for_kv_map)]
#![allow(clippy::used_underscore_binding)]
use std::collections::*;
use std::rc::Rc;
fn main() {
let m: HashMap<u64, u64> = HashMap::new();
for v in m.values() {
//~^ ERROR: you seem to want to iterate on a map's values
//~| NOTE: `-D clippy::for-kv-map` implied by `-D warnings`
2023-07-27 11:40:22 +00:00
let _v = v;
}
let m: Rc<HashMap<u64, u64>> = Rc::new(HashMap::new());
for v in (*m).values() {
//~^ ERROR: you seem to want to iterate on a map's values
2023-07-27 11:40:22 +00:00
let _v = v;
// Here the `*` is not actually necessary, but the test tests that we don't
// suggest
// `in *m.values()` as we used to
}
let mut m: HashMap<u64, u64> = HashMap::new();
for v in m.values_mut() {
//~^ ERROR: you seem to want to iterate on a map's values
2023-07-27 11:40:22 +00:00
let _v = v;
}
let m: &mut HashMap<u64, u64> = &mut HashMap::new();
for v in (*m).values_mut() {
//~^ ERROR: you seem to want to iterate on a map's values
2023-07-27 11:40:22 +00:00
let _v = v;
}
let m: HashMap<u64, u64> = HashMap::new();
let rm = &m;
for k in rm.keys() {
//~^ ERROR: you seem to want to iterate on a map's keys
2023-07-27 11:40:22 +00:00
let _k = k;
}
let m: HashMap<u64, u64> = HashMap::new();
let rm = &m;
'label: for k in rm.keys() {
//~^ ERROR: you seem to want to iterate on a map's keys
let _k = k;
if *k == 0u64 {
break 'label;
}
}
2023-07-27 11:40:22 +00:00
// The following should not produce warnings.
let m: HashMap<u64, u64> = HashMap::new();
// No error, _value is actually used
for (k, _value) in &m {
let _ = _value;
let _k = k;
}
let m: HashMap<u64, String> = Default::default();
for (_, v) in m {
let _v = v;
}
}