needless_collect: Add BinaryHeap for indirect usage lint

This commit is contained in:
Mateusz Gacek 2021-05-04 12:36:20 -07:00
parent 0dc38c047e
commit 1835d8a238
3 changed files with 22 additions and 2 deletions

View File

@ -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;

View File

@ -1,4 +1,4 @@
use std::collections::{HashMap, LinkedList, VecDeque};
use std::collections::{BinaryHeap, HashMap, LinkedList, VecDeque};
fn main() {
let sample = [1; 5];
@ -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 {

View File

@ -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