Skip to content

Commit 344ae11

Browse files
committed
Auto merge of #11329 - unvalley:lint-binary-heap-retain, r=Alexendoo
[`manual_retain`]: add lint case for `binary_heap` Closes #9059 This PR adds changes to perform linting on the `binary_heap` as well, under the [manual_retain rule](https://rust-lang.github.io/rust-clippy/master/index.html#/manual_retain). changelog: [manual_retain]: update for lint `binary_heap`
2 parents 83afad4 + 1eff39d commit 344ae11

File tree

6 files changed

+100
-49
lines changed

6 files changed

+100
-49
lines changed

clippy_lints/src/manual_retain.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ use rustc_semver::RustcVersion;
1212
use rustc_session::{declare_tool_lint, impl_lint_pass};
1313
use rustc_span::symbol::sym;
1414

15-
const ACCEPTABLE_METHODS: [&[&str]; 4] = [
15+
const ACCEPTABLE_METHODS: [&[&str]; 5] = [
16+
&paths::BINARYHEAP_ITER,
1617
&paths::HASHSET_ITER,
1718
&paths::BTREESET_ITER,
1819
&paths::SLICE_INTO,
1920
&paths::VEC_DEQUE_ITER,
2021
];
21-
const ACCEPTABLE_TYPES: [(rustc_span::Symbol, Option<RustcVersion>); 6] = [
22+
const ACCEPTABLE_TYPES: [(rustc_span::Symbol, Option<RustcVersion>); 7] = [
23+
(sym::BinaryHeap, Some(msrvs::BINARY_HEAP_RETAIN)),
2224
(sym::BTreeSet, Some(msrvs::BTREE_SET_RETAIN)),
2325
(sym::BTreeMap, Some(msrvs::BTREE_MAP_RETAIN)),
2426
(sym::HashSet, Some(msrvs::HASH_SET_RETAIN)),

clippy_utils/src/msrvs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ macro_rules! msrv_aliases {
2020
// names may refer to stabilized feature flags or library items
2121
msrv_aliases! {
2222
1,71,0 { TUPLE_ARRAY_CONVERSIONS }
23-
1,70,0 { OPTION_IS_SOME_AND }
23+
1,70,0 { OPTION_IS_SOME_AND, BINARY_HEAP_RETAIN }
2424
1,68,0 { PATH_MAIN_SEPARATOR_STR }
2525
1,65,0 { LET_ELSE, POINTER_CAST_CONSTNESS }
2626
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE }

clippy_utils/src/paths.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [
1515
];
1616
#[cfg(feature = "internal")]
1717
pub const DIAGNOSTIC_BUILDER: [&str; 3] = ["rustc_errors", "diagnostic_builder", "DiagnosticBuilder"];
18+
pub const BINARYHEAP_ITER: [&str; 5] = ["alloc", "collections", "binary_heap", "BinaryHeap", "iter"];
1819
pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "contains_key"];
1920
pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"];
2021
pub const BTREESET_ITER: [&str; 6] = ["alloc", "collections", "btree", "set", "BTreeSet", "iter"];

tests/ui/manual_retain.fixed

+27-12
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,31 @@ fn main() {
1717
}
1818

1919
fn binary_heap_retain() {
20-
// NOTE: Do not lint now, because binary_heap_retain is nightly API.
21-
// And we need to add a test case for msrv if we update this implementation.
22-
// https://github.com/rust-lang/rust/issues/71503
23-
let mut heap = BinaryHeap::from([1, 2, 3]);
24-
heap = heap.into_iter().filter(|x| x % 2 == 0).collect();
25-
heap = heap.iter().filter(|&x| x % 2 == 0).copied().collect();
26-
heap = heap.iter().filter(|&x| x % 2 == 0).cloned().collect();
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);
2725

2826
// Do not lint, because type conversion is performed
29-
heap = heap.into_iter().filter(|x| x % 2 == 0).collect::<BinaryHeap<i8>>();
30-
heap = heap.iter().filter(|&x| x % 2 == 0).copied().collect::<BinaryHeap<i8>>();
31-
heap = heap.iter().filter(|&x| x % 2 == 0).cloned().collect::<BinaryHeap<i8>>();
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>>();
3241

3342
// Do not lint, because this expression is not assign.
34-
let mut bar: BinaryHeap<i8> = heap.iter().filter(|&x| x % 2 == 0).copied().collect();
35-
let mut foobar: BinaryHeap<i8> = heap.into_iter().filter(|x| x % 2 == 0).collect();
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();
3645

3746
// Do not lint, because it is an assignment to a different variable.
3847
bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
@@ -209,6 +218,12 @@ fn vec_deque_retain() {
209218
bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
210219
}
211220

221+
#[clippy::msrv = "1.69"]
222+
fn _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+
212227
#[clippy::msrv = "1.52"]
213228
fn _msrv_153() {
214229
let mut btree_map: BTreeMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();

tests/ui/manual_retain.rs

+27-12
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,31 @@ fn main() {
1717
}
1818

1919
fn binary_heap_retain() {
20-
// NOTE: Do not lint now, because binary_heap_retain is nightly API.
21-
// And we need to add a test case for msrv if we update this implementation.
22-
// https://github.com/rust-lang/rust/issues/71503
23-
let mut heap = BinaryHeap::from([1, 2, 3]);
24-
heap = heap.into_iter().filter(|x| x % 2 == 0).collect();
25-
heap = heap.iter().filter(|&x| x % 2 == 0).copied().collect();
26-
heap = heap.iter().filter(|&x| x % 2 == 0).cloned().collect();
20+
let mut binary_heap = BinaryHeap::from([1, 2, 3]);
21+
// Do lint.
22+
binary_heap = binary_heap.into_iter().filter(|x| x % 2 == 0).collect();
23+
binary_heap = binary_heap.iter().filter(|&x| x % 2 == 0).copied().collect();
24+
binary_heap = binary_heap.iter().filter(|&x| x % 2 == 0).cloned().collect();
2725

2826
// Do not lint, because type conversion is performed
29-
heap = heap.into_iter().filter(|x| x % 2 == 0).collect::<BinaryHeap<i8>>();
30-
heap = heap.iter().filter(|&x| x % 2 == 0).copied().collect::<BinaryHeap<i8>>();
31-
heap = heap.iter().filter(|&x| x % 2 == 0).cloned().collect::<BinaryHeap<i8>>();
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>>();
3241

3342
// Do not lint, because this expression is not assign.
34-
let mut bar: BinaryHeap<i8> = heap.iter().filter(|&x| x % 2 == 0).copied().collect();
35-
let mut foobar: BinaryHeap<i8> = heap.into_iter().filter(|x| x % 2 == 0).collect();
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();
3645

3746
// Do not lint, because it is an assignment to a different variable.
3847
bar = foobar.iter().filter(|&x| x % 2 == 0).copied().collect();
@@ -215,6 +224,12 @@ fn vec_deque_retain() {
215224
bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
216225
}
217226

227+
#[clippy::msrv = "1.69"]
228+
fn _msrv_169() {
229+
let mut binary_heap = BinaryHeap::from([1, 2, 3]);
230+
binary_heap = binary_heap.into_iter().filter(|x| x % 2 == 0).collect();
231+
}
232+
218233
#[clippy::msrv = "1.52"]
219234
fn _msrv_153() {
220235
let mut btree_map: BTreeMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();

tests/ui/manual_retain.stderr

+40-22
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
error: this expression can be written more simply using `.retain()`
2-
--> $DIR/manual_retain.rs:45:5
2+
--> $DIR/manual_retain.rs:22:5
33
|
4-
LL | btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_map.retain(|k, _| k % 2 == 0)`
4+
LL | binary_heap = binary_heap.into_iter().filter(|x| x % 2 == 0).collect();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `binary_heap.retain(|x| x % 2 == 0)`
66
|
77
= note: `-D clippy::manual-retain` implied by `-D warnings`
88

99
error: this expression can be written more simply using `.retain()`
10-
--> $DIR/manual_retain.rs:46:5
10+
--> $DIR/manual_retain.rs:23:5
11+
|
12+
LL | binary_heap = binary_heap.iter().filter(|&x| x % 2 == 0).copied().collect();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `binary_heap.retain(|x| x % 2 == 0)`
14+
15+
error: this expression can be written more simply using `.retain()`
16+
--> $DIR/manual_retain.rs:24:5
17+
|
18+
LL | binary_heap = binary_heap.iter().filter(|&x| x % 2 == 0).cloned().collect();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `binary_heap.retain(|x| x % 2 == 0)`
20+
21+
error: this expression can be written more simply using `.retain()`
22+
--> $DIR/manual_retain.rs:54:5
23+
|
24+
LL | btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_map.retain(|k, _| k % 2 == 0)`
26+
27+
error: this expression can be written more simply using `.retain()`
28+
--> $DIR/manual_retain.rs:55:5
1129
|
1230
LL | btree_map = btree_map.into_iter().filter(|(_, v)| v % 2 == 0).collect();
1331
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_map.retain(|_, &mut v| v % 2 == 0)`
1432

1533
error: this expression can be written more simply using `.retain()`
16-
--> $DIR/manual_retain.rs:47:5
34+
--> $DIR/manual_retain.rs:56:5
1735
|
1836
LL | / btree_map = btree_map
1937
LL | | .into_iter()
@@ -22,37 +40,37 @@ LL | | .collect();
2240
| |__________________^ help: consider calling `.retain()` instead: `btree_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0))`
2341

2442
error: this expression can be written more simply using `.retain()`
25-
--> $DIR/manual_retain.rs:69:5
43+
--> $DIR/manual_retain.rs:78:5
2644
|
2745
LL | btree_set = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
2846
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)`
2947

3048
error: this expression can be written more simply using `.retain()`
31-
--> $DIR/manual_retain.rs:70:5
49+
--> $DIR/manual_retain.rs:79:5
3250
|
3351
LL | btree_set = btree_set.iter().filter(|&x| x % 2 == 0).cloned().collect();
3452
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)`
3553

3654
error: this expression can be written more simply using `.retain()`
37-
--> $DIR/manual_retain.rs:71:5
55+
--> $DIR/manual_retain.rs:80:5
3856
|
3957
LL | btree_set = btree_set.into_iter().filter(|x| x % 2 == 0).collect();
4058
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)`
4159

4260
error: this expression can be written more simply using `.retain()`
43-
--> $DIR/manual_retain.rs:101:5
61+
--> $DIR/manual_retain.rs:110:5
4462
|
4563
LL | hash_map = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
4664
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_map.retain(|k, _| k % 2 == 0)`
4765

4866
error: this expression can be written more simply using `.retain()`
49-
--> $DIR/manual_retain.rs:102:5
67+
--> $DIR/manual_retain.rs:111:5
5068
|
5169
LL | hash_map = hash_map.into_iter().filter(|(_, v)| v % 2 == 0).collect();
5270
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_map.retain(|_, &mut v| v % 2 == 0)`
5371

5472
error: this expression can be written more simply using `.retain()`
55-
--> $DIR/manual_retain.rs:103:5
73+
--> $DIR/manual_retain.rs:112:5
5674
|
5775
LL | / hash_map = hash_map
5876
LL | | .into_iter()
@@ -61,64 +79,64 @@ LL | | .collect();
6179
| |__________________^ help: consider calling `.retain()` instead: `hash_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0))`
6280

6381
error: this expression can be written more simply using `.retain()`
64-
--> $DIR/manual_retain.rs:124:5
82+
--> $DIR/manual_retain.rs:133:5
6583
|
6684
LL | hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect();
6785
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)`
6886

6987
error: this expression can be written more simply using `.retain()`
70-
--> $DIR/manual_retain.rs:125:5
88+
--> $DIR/manual_retain.rs:134:5
7189
|
7290
LL | hash_set = hash_set.iter().filter(|&x| x % 2 == 0).copied().collect();
7391
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)`
7492

7593
error: this expression can be written more simply using `.retain()`
76-
--> $DIR/manual_retain.rs:126:5
94+
--> $DIR/manual_retain.rs:135:5
7795
|
7896
LL | hash_set = hash_set.iter().filter(|&x| x % 2 == 0).cloned().collect();
7997
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)`
8098

8199
error: this expression can be written more simply using `.retain()`
82-
--> $DIR/manual_retain.rs:155:5
100+
--> $DIR/manual_retain.rs:164:5
83101
|
84102
LL | s = s.chars().filter(|&c| c != 'o').to_owned().collect();
85103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `s.retain(|c| c != 'o')`
86104

87105
error: this expression can be written more simply using `.retain()`
88-
--> $DIR/manual_retain.rs:167:5
106+
--> $DIR/manual_retain.rs:176:5
89107
|
90108
LL | vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect();
91109
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)`
92110

93111
error: this expression can be written more simply using `.retain()`
94-
--> $DIR/manual_retain.rs:168:5
112+
--> $DIR/manual_retain.rs:177:5
95113
|
96114
LL | vec = vec.iter().filter(|&x| x % 2 == 0).cloned().collect();
97115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)`
98116

99117
error: this expression can be written more simply using `.retain()`
100-
--> $DIR/manual_retain.rs:169:5
118+
--> $DIR/manual_retain.rs:178:5
101119
|
102120
LL | vec = vec.into_iter().filter(|x| x % 2 == 0).collect();
103121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)`
104122

105123
error: this expression can be written more simply using `.retain()`
106-
--> $DIR/manual_retain.rs:191:5
124+
--> $DIR/manual_retain.rs:200:5
107125
|
108126
LL | vec_deque = vec_deque.iter().filter(|&x| x % 2 == 0).copied().collect();
109127
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)`
110128

111129
error: this expression can be written more simply using `.retain()`
112-
--> $DIR/manual_retain.rs:192:5
130+
--> $DIR/manual_retain.rs:201:5
113131
|
114132
LL | vec_deque = vec_deque.iter().filter(|&x| x % 2 == 0).cloned().collect();
115133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)`
116134

117135
error: this expression can be written more simply using `.retain()`
118-
--> $DIR/manual_retain.rs:193:5
136+
--> $DIR/manual_retain.rs:202:5
119137
|
120138
LL | vec_deque = vec_deque.into_iter().filter(|x| x % 2 == 0).collect();
121139
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)`
122140

123-
error: aborting due to 19 previous errors
141+
error: aborting due to 22 previous errors
124142

0 commit comments

Comments
 (0)