Skip to content

Commit 171789e

Browse files
committed
needless_collect: Lint LinkedList and BinaryHeap in direct usage.
Those two types are supported already when used indirectly. This commit adds support for direct usage as well.
1 parent 59ccc1e commit 171789e

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

clippy_lints/src/loops/needless_collect.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
3131
let is_empty_sugg = Some("next().is_none()".to_string());
3232
let method_name = &*method.ident.name.as_str();
3333
let sugg = if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
34-
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) {
34+
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
35+
is_type_diagnostic_item(cx, ty, sym::LinkedList) ||
36+
is_type_diagnostic_item(cx, ty, sym::BinaryHeap) {
3537
match method_name {
3638
"len" => Some("count()".to_string()),
3739
"is_empty" => is_empty_sugg,

tests/ui/needless_collect.fixed

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#![allow(unused, clippy::suspicious_map, clippy::iter_count)]
44

5-
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
5+
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList};
66

77
#[warn(clippy::needless_collect)]
88
#[allow(unused_variables, clippy::iter_cloned_collect, clippy::iter_next_slice)]
@@ -24,4 +24,13 @@ fn main() {
2424
sample.iter().collect::<HashSet<_>>().len();
2525
// Neither should this
2626
sample.iter().collect::<BTreeSet<_>>().len();
27+
28+
sample.iter().count();
29+
sample.iter().next().is_none();
30+
sample.iter().cloned().any(|x| x == 1);
31+
sample.iter().any(|x| x == &1);
32+
33+
// `BinaryHeap` doesn't have `contains` method
34+
sample.iter().count();
35+
sample.iter().next().is_none();
2736
}

tests/ui/needless_collect.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#![allow(unused, clippy::suspicious_map, clippy::iter_count)]
44

5-
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
5+
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList};
66

77
#[warn(clippy::needless_collect)]
88
#[allow(unused_variables, clippy::iter_cloned_collect, clippy::iter_next_slice)]
@@ -24,4 +24,13 @@ fn main() {
2424
sample.iter().collect::<HashSet<_>>().len();
2525
// Neither should this
2626
sample.iter().collect::<BTreeSet<_>>().len();
27+
28+
sample.iter().collect::<LinkedList<_>>().len();
29+
sample.iter().collect::<LinkedList<_>>().is_empty();
30+
sample.iter().cloned().collect::<LinkedList<_>>().contains(&1);
31+
sample.iter().collect::<LinkedList<_>>().contains(&&1);
32+
33+
// `BinaryHeap` doesn't have `contains` method
34+
sample.iter().collect::<BinaryHeap<_>>().len();
35+
sample.iter().collect::<BinaryHeap<_>>().is_empty();
2736
}

tests/ui/needless_collect.stderr

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,41 @@ error: avoid using `collect()` when not needed
3030
LL | sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().is_empty();
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
3232

33-
error: aborting due to 5 previous errors
33+
error: avoid using `collect()` when not needed
34+
--> $DIR/needless_collect.rs:28:19
35+
|
36+
LL | sample.iter().collect::<LinkedList<_>>().len();
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
38+
39+
error: avoid using `collect()` when not needed
40+
--> $DIR/needless_collect.rs:29:19
41+
|
42+
LL | sample.iter().collect::<LinkedList<_>>().is_empty();
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
44+
45+
error: avoid using `collect()` when not needed
46+
--> $DIR/needless_collect.rs:30:28
47+
|
48+
LL | sample.iter().cloned().collect::<LinkedList<_>>().contains(&1);
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == 1)`
50+
51+
error: avoid using `collect()` when not needed
52+
--> $DIR/needless_collect.rs:31:19
53+
|
54+
LL | sample.iter().collect::<LinkedList<_>>().contains(&&1);
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == &1)`
56+
57+
error: avoid using `collect()` when not needed
58+
--> $DIR/needless_collect.rs:34:19
59+
|
60+
LL | sample.iter().collect::<BinaryHeap<_>>().len();
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
62+
63+
error: avoid using `collect()` when not needed
64+
--> $DIR/needless_collect.rs:35:19
65+
|
66+
LL | sample.iter().collect::<BinaryHeap<_>>().is_empty();
67+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
68+
69+
error: aborting due to 11 previous errors
3470

0 commit comments

Comments
 (0)