Skip to content

Commit 980650e

Browse files
committed
needless_collect: fix suggestion, make test rustfixable
1 parent 7f822e7 commit 980650e

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

clippy_lints/src/loops.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,12 +2427,17 @@ fn check_needless_collect<'a, 'tcx>(expr: &'tcx Expr, cx: &LateContext<'a, 'tcx>
24272427
let contains_arg = snippet(cx, args[1].span, "??");
24282428
let span = shorten_needless_collect_span(expr);
24292429
span_lint_and_then(cx, NEEDLESS_COLLECT, span, NEEDLESS_COLLECT_MSG, |db| {
2430+
let (arg, pred) = if contains_arg.starts_with('&') {
2431+
("x", &contains_arg[1..])
2432+
} else {
2433+
("&x", &*contains_arg)
2434+
};
24302435
db.span_suggestion(
24312436
span,
24322437
"replace with",
24332438
format!(
2434-
".any(|&x| x == {})",
2435-
if contains_arg.starts_with('&') { &contains_arg[1..] } else { &contains_arg }
2439+
".any(|{}| x == {})",
2440+
arg, pred
24362441
),
24372442
Applicability::MachineApplicable,
24382443
);

tests/ui/needless_collect.fixed

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-rustfix
2+
3+
#![allow(unused, clippy::suspicious_map)]
4+
5+
use std::collections::{BTreeSet, HashMap, HashSet};
6+
7+
#[warn(clippy::needless_collect)]
8+
#[allow(unused_variables, clippy::iter_cloned_collect)]
9+
fn main() {
10+
let sample = [1; 5];
11+
let len = sample.iter().count();
12+
if sample.iter().next().is_none() {
13+
// Empty
14+
}
15+
sample.iter().cloned().any(|x| x == 1);
16+
sample.iter().map(|x| (x, x)).count();
17+
// Notice the `HashSet`--this should not be linted
18+
sample.iter().collect::<HashSet<_>>().len();
19+
// Neither should this
20+
sample.iter().collect::<BTreeSet<_>>().len();
21+
}

tests/ui/needless_collect.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// run-rustfix
2+
3+
#![allow(unused, clippy::suspicious_map)]
4+
15
use std::collections::{BTreeSet, HashMap, HashSet};
26

37
#[warn(clippy::needless_collect)]

tests/ui/needless_collect.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
error: avoid using `collect()` when not needed
2-
--> $DIR/needless_collect.rs:7:28
2+
--> $DIR/needless_collect.rs:11:28
33
|
44
LL | let len = sample.iter().collect::<Vec<_>>().len();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.count()`
66
|
77
= note: `-D clippy::needless-collect` implied by `-D warnings`
88

99
error: avoid using `collect()` when not needed
10-
--> $DIR/needless_collect.rs:8:21
10+
--> $DIR/needless_collect.rs:12:21
1111
|
1212
LL | if sample.iter().collect::<Vec<_>>().is_empty() {
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.next().is_none()`
1414

1515
error: avoid using `collect()` when not needed
16-
--> $DIR/needless_collect.rs:11:27
16+
--> $DIR/needless_collect.rs:15:27
1717
|
1818
LL | sample.iter().cloned().collect::<Vec<_>>().contains(&1);
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.any(|&x| x == 1)`
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.any(|x| x == 1)`
2020

2121
error: avoid using `collect()` when not needed
22-
--> $DIR/needless_collect.rs:12:34
22+
--> $DIR/needless_collect.rs:16:34
2323
|
2424
LL | sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `.count()`

0 commit comments

Comments
 (0)