summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/iter_kv_map.fixed
blob: 2cbf972fca5fbe9846df9ce4965de54d30be246e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#![warn(clippy::iter_kv_map)]
#![allow(unused_mut, clippy::redundant_clone, clippy::suspicious_map, clippy::map_identity)]

use std::collections::{BTreeMap, HashMap};

fn main() {
    let get_key = |(key, _val)| key;
    fn ref_acceptor(v: &u32) -> u32 {
        *v
    }

    let map: HashMap<u32, u32> = HashMap::new();

    let _ = map.keys().collect::<Vec<_>>();
    let _ = map.values().collect::<Vec<_>>();
    let _ = map.values().map(|v| v + 2).collect::<Vec<_>>();

    let _ = map.clone().into_keys().collect::<Vec<_>>();
    let _ = map.clone().into_keys().map(|key| key + 2).collect::<Vec<_>>();

    let _ = map.clone().into_values().collect::<Vec<_>>();
    let _ = map.clone().into_values().map(|val| val + 2).collect::<Vec<_>>();

    let _ = map.clone().values().collect::<Vec<_>>();
    let _ = map.keys().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.keys().map(|key| key * 9).count();
    let _ = map.values().map(|value| value * 17).count();

    // Preserve the ref in the fix.
    let _ = map.clone().into_values().map(|ref val| ref_acceptor(val)).count();

    // Preserve the mut in the fix.
    let _ = map
        .clone().into_values().map(|mut val| {
            val += 2;
            val
        })
        .count();

    // Don't let a mut interfere.
    let _ = map.clone().into_values().count();

    let map: BTreeMap<u32, u32> = BTreeMap::new();

    let _ = map.keys().collect::<Vec<_>>();
    let _ = map.values().collect::<Vec<_>>();
    let _ = map.values().map(|v| v + 2).collect::<Vec<_>>();

    let _ = map.clone().into_keys().collect::<Vec<_>>();
    let _ = map.clone().into_keys().map(|key| key + 2).collect::<Vec<_>>();

    let _ = map.clone().into_values().collect::<Vec<_>>();
    let _ = map.clone().into_values().map(|val| val + 2).collect::<Vec<_>>();

    let _ = map.clone().values().collect::<Vec<_>>();
    let _ = map.keys().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.keys().map(|key| key * 9).count();
    let _ = map.values().map(|value| value * 17).count();

    // Preserve the ref in the fix.
    let _ = map.clone().into_values().map(|ref val| ref_acceptor(val)).count();

    // Preserve the mut in the fix.
    let _ = map
        .clone().into_values().map(|mut val| {
            val += 2;
            val
        })
        .count();

    // Don't let a mut interfere.
    let _ = map.clone().into_values().count();
}

#[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.keys().collect::<Vec<_>>();
    //~^ ERROR: iterating on a map's keys
    let _ = map.values().collect::<Vec<_>>();
    //~^ ERROR: iterating on a map's values
    let _ = map.values().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_keys().collect::<Vec<_>>();
    //~^ ERROR: iterating on a map's keys
    let _ = map.clone().into_keys().map(|key| key + 2).collect::<Vec<_>>();
    //~^ ERROR: iterating on a map's keys

    let _ = map.clone().into_values().collect::<Vec<_>>();
    //~^ ERROR: iterating on a map's values
    let _ = map.clone().into_values().map(|val| val + 2).collect::<Vec<_>>();
    //~^ ERROR: iterating on a map's values

    let _ = map.keys().collect::<Vec<_>>();
    //~^ ERROR: iterating on a map's keys
    let _ = map.values().collect::<Vec<_>>();
    //~^ ERROR: iterating on a map's values
    let _ = map.values().map(|v| v + 2).collect::<Vec<_>>();
    //~^ ERROR: iterating on a map's values
}