summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/manual_retain.fixed
blob: 4dea3e8bfe68ad28d75c4f6e2d8ad09933e5c579 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#![warn(clippy::manual_retain)]
#![allow(unused, clippy::redundant_clone)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

fn main() {
    binary_heap_retain();
    btree_set_retain();
    btree_map_retain();
    hash_set_retain();
    hash_map_retain();
    string_retain();
    vec_deque_retain();
    vec_retain();
    _msrv_153();
    _msrv_126();
    _msrv_118();
}

fn binary_heap_retain() {
    let mut binary_heap = BinaryHeap::from([1, 2, 3]);
    // Do lint.
    binary_heap.retain(|x| x % 2 == 0);
    binary_heap.retain(|x| x % 2 == 0);
    binary_heap.retain(|x| x % 2 == 0);

    // Do not lint, because type conversion is performed
    binary_heap = binary_heap
        .into_iter()
        .filter(|x| x % 2 == 0)
        .collect::<BinaryHeap<i8>>();
    binary_heap = binary_heap
        .iter()
        .filter(|&x| x % 2 == 0)
        .copied()
        .collect::<BinaryHeap<i8>>();
    binary_heap = binary_heap
        .iter()
        .filter(|&x| x % 2 == 0)
        .cloned()
        .collect::<BinaryHeap<i8>>();

    // Do not lint, because this expression is not assign.
    let mut bar: BinaryHeap<i8> = binary_heap.iter().filter(|&x| x % 2 == 0).copied().collect();
    let mut foobar: BinaryHeap<i8> = binary_heap.into_iter().filter(|x| x % 2 == 0).collect();

    // Do not lint, because it is an assignment to a different variable.
    bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
    bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}

fn btree_map_retain() {
    let mut btree_map: BTreeMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
    // Do lint.
    btree_map.retain(|k, _| k % 2 == 0);
    btree_map.retain(|_, &mut v| v % 2 == 0);
    btree_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0));

    // Do not lint.
    btree_map = btree_map
        .into_iter()
        .filter(|(x, _)| x % 2 == 0)
        .collect::<BTreeMap<i8, i8>>();

    // Do not lint, because this expression is not assign.
    let mut foobar: BTreeMap<i8, i8> = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();

    // Do not lint, because it is an assignment to a different variable.
    btree_map = foobar.into_iter().filter(|(k, _)| k % 2 == 0).collect();
}

fn btree_set_retain() {
    let mut btree_set = BTreeSet::from([1, 2, 3, 4, 5, 6]);

    // Do lint.
    btree_set.retain(|x| x % 2 == 0);
    btree_set.retain(|x| x % 2 == 0);
    btree_set.retain(|x| x % 2 == 0);

    // Do not lint, because type conversion is performed
    btree_set = btree_set
        .iter()
        .filter(|&x| x % 2 == 0)
        .copied()
        .collect::<BTreeSet<i8>>();

    btree_set = btree_set
        .iter()
        .filter(|&x| x % 2 == 0)
        .cloned()
        .collect::<BTreeSet<i8>>();

    btree_set = btree_set.into_iter().filter(|x| x % 2 == 0).collect::<BTreeSet<i8>>();

    // Do not lint, because this expression is not assign.
    let mut foobar: BTreeSet<i8> = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
    let mut bar: BTreeSet<i8> = btree_set.into_iter().filter(|x| x % 2 == 0).collect();

    // Do not lint, because it is an assignment to a different variable.
    bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
    bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
    bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}

fn hash_map_retain() {
    let mut hash_map: HashMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
    // Do lint.
    hash_map.retain(|k, _| k % 2 == 0);
    hash_map.retain(|_, &mut v| v % 2 == 0);
    hash_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0));

    // Do not lint.
    hash_map = hash_map
        .into_iter()
        .filter(|(x, _)| x % 2 == 0)
        .collect::<HashMap<i8, i8>>();

    // Do not lint, because this expression is not assign.
    let mut foobar: HashMap<i8, i8> = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();

    // Do not lint, because it is an assignment to a different variable.
    hash_map = foobar.into_iter().filter(|(k, _)| k % 2 == 0).collect();
}

fn hash_set_retain() {
    let mut hash_set = HashSet::from([1, 2, 3, 4, 5, 6]);
    // Do lint.
    hash_set.retain(|x| x % 2 == 0);
    hash_set.retain(|x| x % 2 == 0);
    hash_set.retain(|x| x % 2 == 0);

    // Do not lint, because type conversion is performed
    hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect::<HashSet<i8>>();
    hash_set = hash_set
        .iter()
        .filter(|&x| x % 2 == 0)
        .copied()
        .collect::<HashSet<i8>>();

    hash_set = hash_set
        .iter()
        .filter(|&x| x % 2 == 0)
        .cloned()
        .collect::<HashSet<i8>>();

    // Do not lint, because this expression is not assign.
    let mut bar: HashSet<i8> = hash_set.iter().filter(|&x| x % 2 == 0).copied().collect();
    let mut foobar: HashSet<i8> = hash_set.into_iter().filter(|x| x % 2 == 0).collect();

    // Do not lint, because it is an assignment to a different variable.
    bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
    bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
    bar = foobar.into_iter().filter(|&x| x % 2 == 0).collect();
}

fn string_retain() {
    let mut s = String::from("foobar");
    // Do lint.
    s.retain(|c| c != 'o');

    // Do not lint, because this expression is not assign.
    let mut bar: String = s.chars().filter(|&c| c != 'o').to_owned().collect();

    // Do not lint, because it is an assignment to a different variable.
    s = bar.chars().filter(|&c| c != 'o').to_owned().collect();
}

fn vec_retain() {
    let mut vec = vec![0, 1, 2];
    // Do lint.
    vec.retain(|x| x % 2 == 0);
    vec.retain(|x| x % 2 == 0);
    vec.retain(|x| x % 2 == 0);

    // Do not lint, because type conversion is performed
    vec = vec.into_iter().filter(|x| x % 2 == 0).collect::<Vec<i8>>();
    vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect::<Vec<i8>>();
    vec = vec.iter().filter(|&x| x % 2 == 0).cloned().collect::<Vec<i8>>();

    // Do not lint, because this expression is not assign.
    let mut bar: Vec<i8> = vec.iter().filter(|&x| x % 2 == 0).copied().collect();
    let mut foobar: Vec<i8> = vec.into_iter().filter(|x| x % 2 == 0).collect();

    // Do not lint, because it is an assignment to a different variable.
    bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
    bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
    bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}

fn vec_deque_retain() {
    let mut vec_deque = VecDeque::new();
    vec_deque.extend(1..5);

    // Do lint.
    vec_deque.retain(|x| x % 2 == 0);
    vec_deque.retain(|x| x % 2 == 0);
    vec_deque.retain(|x| x % 2 == 0);

    // Do not lint, because type conversion is performed
    vec_deque = vec_deque
        .iter()
        .filter(|&x| x % 2 == 0)
        .copied()
        .collect::<VecDeque<i8>>();
    vec_deque = vec_deque
        .iter()
        .filter(|&x| x % 2 == 0)
        .cloned()
        .collect::<VecDeque<i8>>();
    vec_deque = vec_deque.into_iter().filter(|x| x % 2 == 0).collect::<VecDeque<i8>>();

    // Do not lint, because this expression is not assign.
    let mut bar: VecDeque<i8> = vec_deque.iter().filter(|&x| x % 2 == 0).copied().collect();
    let mut foobar: VecDeque<i8> = vec_deque.into_iter().filter(|x| x % 2 == 0).collect();

    // Do not lint, because it is an assignment to a different variable.
    bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
    bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
    bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}

#[clippy::msrv = "1.69"]
fn _msrv_169() {
    let mut binary_heap = BinaryHeap::from([1, 2, 3]);
    binary_heap = binary_heap.into_iter().filter(|x| x % 2 == 0).collect();
}

#[clippy::msrv = "1.52"]
fn _msrv_153() {
    let mut btree_map: BTreeMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
    btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();

    let mut btree_set = BTreeSet::from([1, 2, 3, 4, 5, 6]);
    btree_set = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
}

#[clippy::msrv = "1.25"]
fn _msrv_126() {
    let mut s = String::from("foobar");
    s = s.chars().filter(|&c| c != 'o').to_owned().collect();
}

#[clippy::msrv = "1.17"]
fn _msrv_118() {
    let mut hash_set = HashSet::from([1, 2, 3, 4, 5, 6]);
    hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect();
    let mut hash_map: HashMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
    hash_map = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
}