fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
8.5 KiB248 linesraw
1#![warn(clippy::manual_retain)]
2#![allow(unused, clippy::redundant_clone)]
3use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
4
5fn main() {
6    binary_heap_retain();
7    btree_set_retain();
8    btree_map_retain();
9    hash_set_retain();
10    hash_map_retain();
11    string_retain();
12    vec_deque_retain();
13    vec_retain();
14    _msrv_153();
15    _msrv_126();
16    _msrv_118();
17}
18
19fn binary_heap_retain() {
20    let mut binary_heap = BinaryHeap::from([1, 2, 3]);
21    // Do lint.
22    binary_heap.retain(|x| x % 2 == 0);
23    binary_heap.retain(|x| x % 2 == 0);
24    binary_heap.retain(|x| x % 2 == 0);
25
26    // Do not lint, because type conversion is performed
27    binary_heap = binary_heap
28        .into_iter()
29        .filter(|x| x % 2 == 0)
30        .collect::<BinaryHeap<i8>>();
31    binary_heap = binary_heap
32        .iter()
33        .filter(|&x| x % 2 == 0)
34        .copied()
35        .collect::<BinaryHeap<i8>>();
36    binary_heap = binary_heap
37        .iter()
38        .filter(|&x| x % 2 == 0)
39        .cloned()
40        .collect::<BinaryHeap<i8>>();
41
42    // Do not lint, because this expression is not assign.
43    let mut bar: BinaryHeap<i8> = binary_heap.iter().filter(|&x| x % 2 == 0).copied().collect();
44    let mut foobar: BinaryHeap<i8> = binary_heap.into_iter().filter(|x| x % 2 == 0).collect();
45
46    // Do not lint, because it is an assignment to a different variable.
47    bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
48    bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
49}
50
51fn btree_map_retain() {
52    let mut btree_map: BTreeMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
53    // Do lint.
54    btree_map.retain(|k, _| k % 2 == 0);
55    btree_map.retain(|_, &mut v| v % 2 == 0);
56    btree_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0));
57
58    // Do not lint.
59    btree_map = btree_map
60        .into_iter()
61        .filter(|(x, _)| x % 2 == 0)
62        .collect::<BTreeMap<i8, i8>>();
63
64    // Do not lint, because this expression is not assign.
65    let mut foobar: BTreeMap<i8, i8> = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
66
67    // Do not lint, because it is an assignment to a different variable.
68    btree_map = foobar.into_iter().filter(|(k, _)| k % 2 == 0).collect();
69}
70
71fn btree_set_retain() {
72    let mut btree_set = BTreeSet::from([1, 2, 3, 4, 5, 6]);
73
74    // Do lint.
75    btree_set.retain(|x| x % 2 == 0);
76    btree_set.retain(|x| x % 2 == 0);
77    btree_set.retain(|x| x % 2 == 0);
78
79    // Do not lint, because type conversion is performed
80    btree_set = btree_set
81        .iter()
82        .filter(|&x| x % 2 == 0)
83        .copied()
84        .collect::<BTreeSet<i8>>();
85
86    btree_set = btree_set
87        .iter()
88        .filter(|&x| x % 2 == 0)
89        .cloned()
90        .collect::<BTreeSet<i8>>();
91
92    btree_set = btree_set.into_iter().filter(|x| x % 2 == 0).collect::<BTreeSet<i8>>();
93
94    // Do not lint, because this expression is not assign.
95    let mut foobar: BTreeSet<i8> = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
96    let mut bar: BTreeSet<i8> = btree_set.into_iter().filter(|x| x % 2 == 0).collect();
97
98    // Do not lint, because it is an assignment to a different variable.
99    bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
100    bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
101    bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
102}
103
104fn hash_map_retain() {
105    let mut hash_map: HashMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
106    // Do lint.
107    hash_map.retain(|k, _| k % 2 == 0);
108    hash_map.retain(|_, &mut v| v % 2 == 0);
109    hash_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0));
110
111    // Do not lint.
112    hash_map = hash_map
113        .into_iter()
114        .filter(|(x, _)| x % 2 == 0)
115        .collect::<HashMap<i8, i8>>();
116
117    // Do not lint, because this expression is not assign.
118    let mut foobar: HashMap<i8, i8> = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
119
120    // Do not lint, because it is an assignment to a different variable.
121    hash_map = foobar.into_iter().filter(|(k, _)| k % 2 == 0).collect();
122}
123
124fn hash_set_retain() {
125    let mut hash_set = HashSet::from([1, 2, 3, 4, 5, 6]);
126    // Do lint.
127    hash_set.retain(|x| x % 2 == 0);
128    hash_set.retain(|x| x % 2 == 0);
129    hash_set.retain(|x| x % 2 == 0);
130
131    // Do not lint, because type conversion is performed
132    hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect::<HashSet<i8>>();
133    hash_set = hash_set
134        .iter()
135        .filter(|&x| x % 2 == 0)
136        .copied()
137        .collect::<HashSet<i8>>();
138
139    hash_set = hash_set
140        .iter()
141        .filter(|&x| x % 2 == 0)
142        .cloned()
143        .collect::<HashSet<i8>>();
144
145    // Do not lint, because this expression is not assign.
146    let mut bar: HashSet<i8> = hash_set.iter().filter(|&x| x % 2 == 0).copied().collect();
147    let mut foobar: HashSet<i8> = hash_set.into_iter().filter(|x| x % 2 == 0).collect();
148
149    // Do not lint, because it is an assignment to a different variable.
150    bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
151    bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
152    bar = foobar.into_iter().filter(|&x| x % 2 == 0).collect();
153}
154
155fn string_retain() {
156    let mut s = String::from("foobar");
157    // Do lint.
158    s.retain(|c| c != 'o');
159
160    // Do not lint, because this expression is not assign.
161    let mut bar: String = s.chars().filter(|&c| c != 'o').to_owned().collect();
162
163    // Do not lint, because it is an assignment to a different variable.
164    s = bar.chars().filter(|&c| c != 'o').to_owned().collect();
165}
166
167fn vec_retain() {
168    let mut vec = vec![0, 1, 2];
169    // Do lint.
170    vec.retain(|x| x % 2 == 0);
171    vec.retain(|x| x % 2 == 0);
172    vec.retain(|x| x % 2 == 0);
173
174    // Do not lint, because type conversion is performed
175    vec = vec.into_iter().filter(|x| x % 2 == 0).collect::<Vec<i8>>();
176    vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect::<Vec<i8>>();
177    vec = vec.iter().filter(|&x| x % 2 == 0).cloned().collect::<Vec<i8>>();
178
179    // Do not lint, because this expression is not assign.
180    let mut bar: Vec<i8> = vec.iter().filter(|&x| x % 2 == 0).copied().collect();
181    let mut foobar: Vec<i8> = vec.into_iter().filter(|x| x % 2 == 0).collect();
182
183    // Do not lint, because it is an assignment to a different variable.
184    bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
185    bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
186    bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
187}
188
189fn vec_deque_retain() {
190    let mut vec_deque = VecDeque::new();
191    vec_deque.extend(1..5);
192
193    // Do lint.
194    vec_deque.retain(|x| x % 2 == 0);
195    vec_deque.retain(|x| x % 2 == 0);
196    vec_deque.retain(|x| x % 2 == 0);
197
198    // Do not lint, because type conversion is performed
199    vec_deque = vec_deque
200        .iter()
201        .filter(|&x| x % 2 == 0)
202        .copied()
203        .collect::<VecDeque<i8>>();
204    vec_deque = vec_deque
205        .iter()
206        .filter(|&x| x % 2 == 0)
207        .cloned()
208        .collect::<VecDeque<i8>>();
209    vec_deque = vec_deque.into_iter().filter(|x| x % 2 == 0).collect::<VecDeque<i8>>();
210
211    // Do not lint, because this expression is not assign.
212    let mut bar: VecDeque<i8> = vec_deque.iter().filter(|&x| x % 2 == 0).copied().collect();
213    let mut foobar: VecDeque<i8> = vec_deque.into_iter().filter(|x| x % 2 == 0).collect();
214
215    // Do not lint, because it is an assignment to a different variable.
216    bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
217    bar = foobar.iter().filter(|&x| x % 2 == 0).cloned().collect();
218    bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
219}
220
221#[clippy::msrv = "1.69"]
222fn _msrv_169() {
223    let mut binary_heap = BinaryHeap::from([1, 2, 3]);
224    binary_heap = binary_heap.into_iter().filter(|x| x % 2 == 0).collect();
225}
226
227#[clippy::msrv = "1.52"]
228fn _msrv_153() {
229    let mut btree_map: BTreeMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
230    btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
231
232    let mut btree_set = BTreeSet::from([1, 2, 3, 4, 5, 6]);
233    btree_set = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
234}
235
236#[clippy::msrv = "1.25"]
237fn _msrv_126() {
238    let mut s = String::from("foobar");
239    s = s.chars().filter(|&c| c != 'o').to_owned().collect();
240}
241
242#[clippy::msrv = "1.17"]
243fn _msrv_118() {
244    let mut hash_set = HashSet::from([1, 2, 3, 4, 5, 6]);
245    hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect();
246    let mut hash_map: HashMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
247    hash_map = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
248}