Skip to content

Commit a21607d

Browse files
committed
needless_collect: For BTreeMap and HashMap lint only is_empty
- `len` might produce different results than `count` - they don't have `contain` but `contains_key` method
1 parent 182a185 commit a21607d

File tree

4 files changed

+63
-33
lines changed

4 files changed

+63
-33
lines changed

clippy_lints/src/loops/needless_collect.rs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ 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,32 +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 {
48-
span_lint_and_sugg(
49-
cx,
50-
NEEDLESS_COLLECT,
51-
method0_span.with_hi(expr.span.hi()),
52-
NEEDLESS_COLLECT_MSG,
53-
"replace with",
54-
sugg,
55-
Applicability::MachineApplicable,
56-
);
31+
let is_empty_sugg = Some("next().is_none()".to_string());
32+
let method_name = &*method.ident.name.as_str();
33+
let sugg = if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
34+
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) {
35+
match method_name {
36+
"len" => Some("count()".to_string()),
37+
"is_empty" => is_empty_sugg,
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+
}
47+
}
48+
else if match_type(cx, ty, &paths::BTREEMAP) ||
49+
is_type_diagnostic_item(cx, ty, sym::hashmap_type) {
50+
match method_name {
51+
"is_empty" => is_empty_sugg,
52+
_ => None,
53+
}
54+
}
55+
else {
56+
None
57+
};
58+
if let Some(sugg) = sugg {
59+
span_lint_and_sugg(
60+
cx,
61+
NEEDLESS_COLLECT,
62+
method0_span.with_hi(expr.span.hi()),
63+
NEEDLESS_COLLECT_MSG,
64+
"replace with",
65+
sugg,
66+
Applicability::MachineApplicable,
67+
);
68+
}
5769
}
5870
}
5971
}

tests/ui/needless_collect.fixed

Lines changed: 8 additions & 2 deletions
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, HashMap, HashSet};
66

77
#[warn(clippy::needless_collect)]
88
#[allow(unused_variables, clippy::iter_cloned_collect, clippy::iter_next_slice)]
@@ -13,7 +13,13 @@ 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

tests/ui/needless_collect.rs

Lines changed: 7 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::{BTreeSet, HashMap, HashSet};
5+
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
66

77
#[warn(clippy::needless_collect)]
88
#[allow(unused_variables, clippy::iter_cloned_collect, clippy::iter_next_slice)]
@@ -13,7 +13,13 @@ 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

tests/ui/needless_collect.stderr

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ 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: aborting due to 5 previous errors
2834

0 commit comments

Comments
 (0)