Skip to content

Commit 10db5a6

Browse files
committed
Auto merge of rust-lang#7188 - mgacek8:issue7164_needless_collect_FP, r=xFrednet,flip1995
`needless_collect` enhancements fixes rust-lang#7164 changelog: `needless_collect`: For `BTreeMap` and `HashMap` lint only `is_empty`, as `len` might produce different results than iter's `count` changelog: `needless_collect`: Lint `LinkedList` and `BinaryHeap` in direct usage case as well
2 parents 08ce8bb + b249290 commit 10db5a6

File tree

4 files changed

+114
-29
lines changed

4 files changed

+114
-29
lines changed

clippy_lints/src/loops/needless_collect.rs

+35-22
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
use super::NEEDLESS_COLLECT;
22
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
3-
use clippy_utils::source::snippet;
3+
use clippy_utils::source::{snippet, snippet_with_applicability};
44
use clippy_utils::sugg::Sugg;
5-
use clippy_utils::ty::{is_type_diagnostic_item, match_type};
6-
use clippy_utils::{is_trait_method, path_to_local_id, paths};
5+
use clippy_utils::ty::is_type_diagnostic_item;
6+
use clippy_utils::{is_trait_method, path_to_local_id};
77
use if_chain::if_chain;
88
use rustc_errors::Applicability;
99
use rustc_hir::intravisit::{walk_block, walk_expr, NestedVisitorMap, Visitor};
1010
use rustc_hir::{Block, Expr, ExprKind, GenericArg, GenericArgs, HirId, Local, Pat, PatKind, QPath, StmtKind, Ty};
1111
use rustc_lint::LateContext;
1212
use rustc_middle::hir::map::Map;
13-
1413
use rustc_span::symbol::{sym, Ident};
1514
use rustc_span::{MultiSpan, Span};
1615

@@ -28,31 +27,45 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
2827
if let Some(generic_args) = chain_method.args;
2928
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
3029
if let Some(ty) = cx.typeck_results().node_type_opt(ty.hir_id);
31-
if is_type_diagnostic_item(cx, ty, sym::vec_type)
32-
|| is_type_diagnostic_item(cx, ty, sym::vecdeque_type)
33-
|| match_type(cx, ty, &paths::BTREEMAP)
34-
|| is_type_diagnostic_item(cx, ty, sym::hashmap_type);
35-
if let Some(sugg) = match &*method.ident.name.as_str() {
36-
"len" => Some("count()".to_string()),
37-
"is_empty" => Some("next().is_none()".to_string()),
38-
"contains" => {
39-
let contains_arg = snippet(cx, args[1].span, "??");
40-
let (arg, pred) = contains_arg
41-
.strip_prefix('&')
42-
.map_or(("&x", &*contains_arg), |s| ("x", s));
43-
Some(format!("any(|{}| x == {})", arg, pred))
44-
}
45-
_ => None,
46-
};
4730
then {
31+
let mut applicability = Applicability::MachineApplicable;
32+
let is_empty_sugg = "next().is_none()".to_string();
33+
let method_name = &*method.ident.name.as_str();
34+
let sugg = if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
35+
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
36+
is_type_diagnostic_item(cx, ty, sym::LinkedList) ||
37+
is_type_diagnostic_item(cx, ty, sym::BinaryHeap) {
38+
match method_name {
39+
"len" => "count()".to_string(),
40+
"is_empty" => is_empty_sugg,
41+
"contains" => {
42+
let contains_arg = snippet_with_applicability(cx, args[1].span, "??", &mut applicability);
43+
let (arg, pred) = contains_arg
44+
.strip_prefix('&')
45+
.map_or(("&x", &*contains_arg), |s| ("x", s));
46+
format!("any(|{}| x == {})", arg, pred)
47+
}
48+
_ => return,
49+
}
50+
}
51+
else if is_type_diagnostic_item(cx, ty, sym::BTreeMap) ||
52+
is_type_diagnostic_item(cx, ty, sym::hashmap_type) {
53+
match method_name {
54+
"is_empty" => is_empty_sugg,
55+
_ => return,
56+
}
57+
}
58+
else {
59+
return;
60+
};
4861
span_lint_and_sugg(
4962
cx,
5063
NEEDLESS_COLLECT,
5164
method0_span.with_hi(expr.span.hi()),
5265
NEEDLESS_COLLECT_MSG,
5366
"replace with",
5467
sugg,
55-
Applicability::MachineApplicable,
68+
applicability,
5669
);
5770
}
5871
}
@@ -86,7 +99,7 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
8699
if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
87100
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
88101
is_type_diagnostic_item(cx, ty, sym::BinaryHeap) ||
89-
match_type(cx, ty, &paths::LINKED_LIST);
102+
is_type_diagnostic_item(cx, ty, sym::LinkedList);
90103
if let Some(iter_calls) = detect_iter_and_into_iters(block, *ident);
91104
if let [iter_call] = &*iter_calls;
92105
then {

tests/ui/needless_collect.fixed

+17-2
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::{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)]
@@ -13,9 +13,24 @@ fn main() {
1313
// Empty
1414
}
1515
sample.iter().cloned().any(|x| x == 1);
16-
sample.iter().map(|x| (x, x)).count();
16+
// #7164 HashMap's and BTreeMap's `len` usage should not be linted
17+
sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
18+
sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().len();
19+
20+
sample.iter().map(|x| (x, x)).next().is_none();
21+
sample.iter().map(|x| (x, x)).next().is_none();
22+
1723
// Notice the `HashSet`--this should not be linted
1824
sample.iter().collect::<HashSet<_>>().len();
1925
// Neither should this
2026
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();
2136
}

tests/ui/needless_collect.rs

+16-1
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::{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)]
@@ -13,9 +13,24 @@ fn main() {
1313
// Empty
1414
}
1515
sample.iter().cloned().collect::<Vec<_>>().contains(&1);
16+
// #7164 HashMap's and BTreeMap's `len` usage should not be linted
1617
sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
18+
sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().len();
19+
20+
sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().is_empty();
21+
sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().is_empty();
22+
1723
// Notice the `HashSet`--this should not be linted
1824
sample.iter().collect::<HashSet<_>>().len();
1925
// Neither should this
2026
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();
2136
}

tests/ui/needless_collect.stderr

+46-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,52 @@ LL | sample.iter().cloned().collect::<Vec<_>>().contains(&1);
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == 1)`
2020

2121
error: avoid using `collect()` when not needed
22-
--> $DIR/needless_collect.rs:16:35
22+
--> $DIR/needless_collect.rs:20:35
2323
|
24-
LL | sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
24+
LL | sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().is_empty();
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
2626

27-
error: aborting due to 4 previous errors
27+
error: avoid using `collect()` when not needed
28+
--> $DIR/needless_collect.rs:21:35
29+
|
30+
LL | sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().is_empty();
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
32+
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
2870

0 commit comments

Comments
 (0)