Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1835d8a

Browse files
committed
needless_collect: Add BinaryHeap for indirect usage lint
1 parent 0dc38c0 commit 1835d8a

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

clippy_lints/src/loops/needless_collect.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
8585
if let ty = cx.typeck_results().node_type(hir_id);
8686
if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
8787
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
88+
is_type_diagnostic_item(cx, ty, sym::BinaryHeap) ||
8889
match_type(cx, ty, &paths::LINKED_LIST);
8990
if let Some(iter_calls) = detect_iter_and_into_iters(block, *ident);
9091
if let [iter_call] = &*iter_calls;

tests/ui/needless_collect_indirect.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{HashMap, LinkedList, VecDeque};
1+
use std::collections::{BinaryHeap, HashMap, LinkedList, VecDeque};
22

33
fn main() {
44
let sample = [1; 5];
@@ -62,6 +62,11 @@ mod issue7110 {
6262
let indirect_len: LinkedList<_> = sample.iter().collect();
6363
indirect_len.len()
6464
}
65+
fn lint_binary_heap() -> usize {
66+
let sample = [1; 5];
67+
let indirect_len: BinaryHeap<_> = sample.iter().collect();
68+
indirect_len.len()
69+
}
6570
fn dont_lint(string: &str) -> usize {
6671
let buffer: Vec<&str> = string.split('/').collect();
6772
for buff in &buffer {

tests/ui/needless_collect_indirect.stderr

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,19 @@ LL |
111111
LL | sample.iter().count()
112112
|
113113

114-
error: aborting due to 8 previous errors
114+
error: avoid using `collect()` when not needed
115+
--> $DIR/needless_collect_indirect.rs:67:57
116+
|
117+
LL | let indirect_len: BinaryHeap<_> = sample.iter().collect();
118+
| ^^^^^^^
119+
LL | indirect_len.len()
120+
| ------------------ the iterator could be used here instead
121+
|
122+
help: take the original Iterator's count instead of collecting it and finding the length
123+
|
124+
LL |
125+
LL | sample.iter().count()
126+
|
127+
128+
error: aborting due to 9 previous errors
115129

0 commit comments

Comments
 (0)