Skip to content

Commit

Permalink
needless_collect: Add BinaryHeap for indirect usage lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mgacek8 committed May 4, 2021
1 parent 0dc38c0 commit 1835d8a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions clippy_lints/src/loops/needless_collect.rs
Expand Up @@ -85,6 +85,7 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
if let ty = cx.typeck_results().node_type(hir_id);
if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
is_type_diagnostic_item(cx, ty, sym::BinaryHeap) ||
match_type(cx, ty, &paths::LINKED_LIST);
if let Some(iter_calls) = detect_iter_and_into_iters(block, *ident);
if let [iter_call] = &*iter_calls;
Expand Down
7 changes: 6 additions & 1 deletion tests/ui/needless_collect_indirect.rs
@@ -1,4 +1,4 @@
use std::collections::{HashMap, LinkedList, VecDeque};
use std::collections::{BinaryHeap, HashMap, LinkedList, VecDeque};

fn main() {
let sample = [1; 5];
Expand Down Expand Up @@ -62,6 +62,11 @@ mod issue7110 {
let indirect_len: LinkedList<_> = sample.iter().collect();
indirect_len.len()
}
fn lint_binary_heap() -> usize {
let sample = [1; 5];
let indirect_len: BinaryHeap<_> = sample.iter().collect();
indirect_len.len()
}
fn dont_lint(string: &str) -> usize {
let buffer: Vec<&str> = string.split('/').collect();
for buff in &buffer {
Expand Down
16 changes: 15 additions & 1 deletion tests/ui/needless_collect_indirect.stderr
Expand Up @@ -111,5 +111,19 @@ LL |
LL | sample.iter().count()
|

error: aborting due to 8 previous errors
error: avoid using `collect()` when not needed
--> $DIR/needless_collect_indirect.rs:67:57
|
LL | let indirect_len: BinaryHeap<_> = sample.iter().collect();
| ^^^^^^^
LL | indirect_len.len()
| ------------------ the iterator could be used here instead
|
help: take the original Iterator's count instead of collecting it and finding the length
|
LL |
LL | sample.iter().count()
|

error: aborting due to 9 previous errors

0 comments on commit 1835d8a

Please sign in to comment.