Skip to content

Commit

Permalink
feat: update manual_retain to lint binary_heap_retain
Browse files Browse the repository at this point in the history
  • Loading branch information
unvalley committed Aug 12, 2023
1 parent bd1554c commit d1abb75
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 34 deletions.
6 changes: 4 additions & 2 deletions clippy_lints/src/manual_retain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use rustc_semver::RustcVersion;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::symbol::sym;

const ACCEPTABLE_METHODS: [&[&str]; 4] = [
const ACCEPTABLE_METHODS: [&[&str]; 5] = [
&paths::BINARYHEAP_ITER,
&paths::HASHSET_ITER,
&paths::BTREESET_ITER,
&paths::SLICE_INTO,
&paths::VEC_DEQUE_ITER,
];
const ACCEPTABLE_TYPES: [(rustc_span::Symbol, Option<RustcVersion>); 6] = [
const ACCEPTABLE_TYPES: [(rustc_span::Symbol, Option<RustcVersion>); 7] = [
(sym::BinaryHeap, Some(msrvs::BINARY_HEAP_RETAIN)),
(sym::BTreeSet, Some(msrvs::BTREE_SET_RETAIN)),
(sym::BTreeMap, Some(msrvs::BTREE_MAP_RETAIN)),
(sym::HashSet, Some(msrvs::HASH_SET_RETAIN)),
Expand Down
2 changes: 1 addition & 1 deletion clippy_utils/src/msrvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ macro_rules! msrv_aliases {
// names may refer to stabilized feature flags or library items
msrv_aliases! {
1,71,0 { TUPLE_ARRAY_CONVERSIONS }
1,70,0 { OPTION_IS_SOME_AND }
1,70,0 { OPTION_IS_SOME_AND, BINARY_HEAP_RETAIN }
1,68,0 { PATH_MAIN_SEPARATOR_STR }
1,65,0 { LET_ELSE, POINTER_CAST_CONSTNESS }
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE }
Expand Down
1 change: 1 addition & 0 deletions clippy_utils/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub const DIAGNOSTIC_BUILDER: [&str; 3] = ["rustc_errors", "diagnostic_builder",
pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "contains_key"];
pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"];
pub const BTREESET_ITER: [&str; 6] = ["alloc", "collections", "btree", "set", "BTreeSet", "iter"];
pub const BINARYHEAP_ITER: [&str; 5] = ["alloc", "collections", "binary_heap", "BinaryHeap", "iter"];
pub const CLONE_TRAIT_METHOD: [&str; 4] = ["core", "clone", "Clone", "clone"];
pub const CORE_ITER_CLONED: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "cloned"];
pub const CORE_ITER_COPIED: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "copied"];
Expand Down
16 changes: 10 additions & 6 deletions tests/ui/manual_retain.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ fn main() {
}

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

// Do not lint, because type conversion is performed
heap = heap.into_iter().filter(|x| x % 2 == 0).collect::<BinaryHeap<i8>>();
Expand Down Expand Up @@ -210,6 +208,12 @@ fn vec_deque_retain() {
bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}

#[clippy::msrv = "1.70"]
fn _msrv_170() {
let mut heap = BinaryHeap::from([1, 2, 3]);
heap.retain(|x| x % 2 == 0);
}

#[clippy::msrv = "1.52"]
fn _msrv_153() {
let mut btree_map: BTreeMap<i8, i8> = (0..8).map(|x| (x, x * 10)).collect();
Expand Down
10 changes: 7 additions & 3 deletions tests/ui/manual_retain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ fn main() {
}

fn binary_heap_retain() {
// NOTE: Do not lint now, because binary_heap_retain is nightly API.
// And we need to add a test case for msrv if we update this implementation.
// https://github.com/rust-lang/rust/issues/71503
let mut heap = BinaryHeap::from([1, 2, 3]);
// Do lint.
heap = heap.into_iter().filter(|x| x % 2 == 0).collect();
heap = heap.iter().filter(|&x| x % 2 == 0).copied().collect();
heap = heap.iter().filter(|&x| x % 2 == 0).cloned().collect();
Expand Down Expand Up @@ -216,6 +214,12 @@ fn vec_deque_retain() {
bar = foobar.into_iter().filter(|x| x % 2 == 0).collect();
}

#[clippy::msrv = "1.70"]
fn _msrv_170() {
let mut heap = BinaryHeap::from([1, 2, 3]);
heap = 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();
Expand Down
68 changes: 46 additions & 22 deletions tests/ui/manual_retain.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:46:5
--> $DIR/manual_retain.rs:23:5
|
LL | btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_map.retain(|k, _| k % 2 == 0)`
LL | heap = heap.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `heap.retain(|x| x % 2 == 0)`
|
= note: `-D clippy::manual-retain` implied by `-D warnings`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:47:5
--> $DIR/manual_retain.rs:24:5
|
LL | heap = heap.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `heap.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:25:5
|
LL | heap = heap.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `heap.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:44:5
|
LL | btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_map.retain(|k, _| k % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:45:5
|
LL | btree_map = btree_map.into_iter().filter(|(_, v)| v % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_map.retain(|_, &mut v| v % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:48:5
--> $DIR/manual_retain.rs:46:5
|
LL | / btree_map = btree_map
LL | | .into_iter()
Expand All @@ -22,37 +40,37 @@ LL | | .collect();
| |__________________^ help: consider calling `.retain()` instead: `btree_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0))`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:70:5
--> $DIR/manual_retain.rs:68:5
|
LL | btree_set = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:71:5
--> $DIR/manual_retain.rs:69:5
|
LL | btree_set = btree_set.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:72:5
--> $DIR/manual_retain.rs:70:5
|
LL | btree_set = btree_set.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:102:5
--> $DIR/manual_retain.rs:100:5
|
LL | hash_map = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_map.retain(|k, _| k % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:103:5
--> $DIR/manual_retain.rs:101:5
|
LL | hash_map = hash_map.into_iter().filter(|(_, v)| v % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_map.retain(|_, &mut v| v % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:104:5
--> $DIR/manual_retain.rs:102:5
|
LL | / hash_map = hash_map
LL | | .into_iter()
Expand All @@ -61,64 +79,70 @@ LL | | .collect();
| |__________________^ help: consider calling `.retain()` instead: `hash_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0))`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:125:5
--> $DIR/manual_retain.rs:123:5
|
LL | hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:126:5
--> $DIR/manual_retain.rs:124:5
|
LL | hash_set = hash_set.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:127:5
--> $DIR/manual_retain.rs:125:5
|
LL | hash_set = hash_set.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:156:5
--> $DIR/manual_retain.rs:154:5
|
LL | s = s.chars().filter(|&c| c != 'o').to_owned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `s.retain(|c| c != 'o')`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:168:5
--> $DIR/manual_retain.rs:166:5
|
LL | vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:169:5
--> $DIR/manual_retain.rs:167:5
|
LL | vec = vec.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:170:5
--> $DIR/manual_retain.rs:168:5
|
LL | vec = vec.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:192:5
--> $DIR/manual_retain.rs:190:5
|
LL | vec_deque = vec_deque.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:193:5
--> $DIR/manual_retain.rs:191:5
|
LL | vec_deque = vec_deque.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)`

error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:194:5
--> $DIR/manual_retain.rs:192:5
|
LL | vec_deque = vec_deque.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)`

error: aborting due to 19 previous errors
error: this expression can be written more simply using `.retain()`
--> $DIR/manual_retain.rs:220:5
|
LL | heap = heap.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `heap.retain(|x| x % 2 == 0)`

error: aborting due to 23 previous errors

0 comments on commit d1abb75

Please sign in to comment.