Skip to content

Commit e629869

Browse files
authored
extend needless_collect (#14361)
changelog: [`needless_collect`]: extend needless_collect to lint more cases Fix #14350
2 parents c23eed6 + 6d7072b commit e629869

File tree

4 files changed

+52
-29
lines changed

4 files changed

+52
-29
lines changed

clippy_lints/src/methods/needless_collect.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ pub(super) fn check<'tcx>(
3838
Node::Expr(parent) => {
3939
check_collect_into_intoiterator(cx, parent, collect_expr, call_span, iter_expr);
4040

41+
let sugg: String;
42+
let mut app;
43+
4144
if let ExprKind::MethodCall(name, _, args @ ([] | [_]), _) = parent.kind {
42-
let mut app = Applicability::MachineApplicable;
45+
app = Applicability::MachineApplicable;
4346
let collect_ty = cx.typeck_results().expr_ty(collect_expr);
4447

45-
let sugg: String = match name.ident.name {
48+
sugg = match name.ident.name {
4649
sym::len => {
4750
if let Some(adt) = collect_ty.ty_adt_def()
4851
&& matches!(
@@ -78,17 +81,23 @@ pub(super) fn check<'tcx>(
7881
},
7982
_ => return,
8083
};
81-
82-
span_lint_and_sugg(
83-
cx,
84-
NEEDLESS_COLLECT,
85-
call_span.with_hi(parent.span.hi()),
86-
NEEDLESS_COLLECT_MSG,
87-
"replace with",
88-
sugg,
89-
app,
90-
);
84+
} else if let ExprKind::Index(_, index, _) = parent.kind {
85+
app = Applicability::MaybeIncorrect;
86+
let snip = snippet_with_applicability(cx, index.span, "_", &mut app);
87+
sugg = format!("nth({snip}).unwrap()");
88+
} else {
89+
return;
9190
}
91+
92+
span_lint_and_sugg(
93+
cx,
94+
NEEDLESS_COLLECT,
95+
call_span.with_hi(parent.span.hi()),
96+
NEEDLESS_COLLECT_MSG,
97+
"replace with",
98+
sugg,
99+
app,
100+
);
92101
},
93102
Node::LetStmt(l) => {
94103
if let PatKind::Binding(BindingMode::NONE | BindingMode::MUT, id, _, None) = l.pat.kind

tests/ui/needless_collect.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ fn main() {
2020
}
2121
sample.iter().cloned().any(|x| x == 1);
2222
//~^ needless_collect
23+
24+
let _ = sample.iter().cloned().nth(1).unwrap();
25+
//~^ needless_collect
26+
2327
// #7164 HashMap's and BTreeMap's `len` usage should not be linted
2428
sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
2529
sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().len();

tests/ui/needless_collect.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ fn main() {
2020
}
2121
sample.iter().cloned().collect::<Vec<_>>().contains(&1);
2222
//~^ needless_collect
23+
24+
let _ = sample.iter().cloned().collect::<Vec<_>>()[1];
25+
//~^ needless_collect
26+
2327
// #7164 HashMap's and BTreeMap's `len` usage should not be linted
2428
sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
2529
sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().len();

tests/ui/needless_collect.stderr

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,100 +20,106 @@ LL | sample.iter().cloned().collect::<Vec<_>>().contains(&1);
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == 1)`
2121

2222
error: avoid using `collect()` when not needed
23-
--> tests/ui/needless_collect.rs:27:35
23+
--> tests/ui/needless_collect.rs:24:36
24+
|
25+
LL | let _ = sample.iter().cloned().collect::<Vec<_>>()[1];
26+
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `nth(1).unwrap()`
27+
28+
error: avoid using `collect()` when not needed
29+
--> tests/ui/needless_collect.rs:31:35
2430
|
2531
LL | sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().is_empty();
2632
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
2733

2834
error: avoid using `collect()` when not needed
29-
--> tests/ui/needless_collect.rs:29:35
35+
--> tests/ui/needless_collect.rs:33:35
3036
|
3137
LL | sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().is_empty();
3238
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
3339

3440
error: avoid using `collect()` when not needed
35-
--> tests/ui/needless_collect.rs:37:19
41+
--> tests/ui/needless_collect.rs:41:19
3642
|
3743
LL | sample.iter().collect::<LinkedList<_>>().len();
3844
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
3945

4046
error: avoid using `collect()` when not needed
41-
--> tests/ui/needless_collect.rs:39:19
47+
--> tests/ui/needless_collect.rs:43:19
4248
|
4349
LL | sample.iter().collect::<LinkedList<_>>().is_empty();
4450
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
4551

4652
error: avoid using `collect()` when not needed
47-
--> tests/ui/needless_collect.rs:41:28
53+
--> tests/ui/needless_collect.rs:45:28
4854
|
4955
LL | sample.iter().cloned().collect::<LinkedList<_>>().contains(&1);
5056
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == 1)`
5157

5258
error: avoid using `collect()` when not needed
53-
--> tests/ui/needless_collect.rs:43:19
59+
--> tests/ui/needless_collect.rs:47:19
5460
|
5561
LL | sample.iter().collect::<LinkedList<_>>().contains(&&1);
5662
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == &1)`
5763

5864
error: avoid using `collect()` when not needed
59-
--> tests/ui/needless_collect.rs:47:19
65+
--> tests/ui/needless_collect.rs:51:19
6066
|
6167
LL | sample.iter().collect::<BinaryHeap<_>>().len();
6268
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
6369

6470
error: avoid using `collect()` when not needed
65-
--> tests/ui/needless_collect.rs:49:19
71+
--> tests/ui/needless_collect.rs:53:19
6672
|
6773
LL | sample.iter().collect::<BinaryHeap<_>>().is_empty();
6874
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
6975

7076
error: avoid using `collect()` when not needed
71-
--> tests/ui/needless_collect.rs:55:27
77+
--> tests/ui/needless_collect.rs:59:27
7278
|
7379
LL | let _ = sample.iter().collect::<HashSet<_>>().is_empty();
7480
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
7581

7682
error: avoid using `collect()` when not needed
77-
--> tests/ui/needless_collect.rs:57:27
83+
--> tests/ui/needless_collect.rs:61:27
7884
|
7985
LL | let _ = sample.iter().collect::<HashSet<_>>().contains(&&0);
8086
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == &0)`
8187

8288
error: avoid using `collect()` when not needed
83-
--> tests/ui/needless_collect.rs:80:27
89+
--> tests/ui/needless_collect.rs:84:27
8490
|
8591
LL | let _ = sample.iter().collect::<VecWrapper<_>>().is_empty();
8692
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
8793

8894
error: avoid using `collect()` when not needed
89-
--> tests/ui/needless_collect.rs:82:27
95+
--> tests/ui/needless_collect.rs:86:27
9096
|
9197
LL | let _ = sample.iter().collect::<VecWrapper<_>>().contains(&&0);
9298
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == &0)`
9399

94100
error: avoid using `collect()` when not needed
95-
--> tests/ui/needless_collect.rs:87:40
101+
--> tests/ui/needless_collect.rs:91:40
96102
|
97103
LL | Vec::<u8>::new().extend((0..10).collect::<Vec<_>>());
98104
| ^^^^^^^^^^^^^^^^^^^^ help: remove this call
99105

100106
error: avoid using `collect()` when not needed
101-
--> tests/ui/needless_collect.rs:89:20
107+
--> tests/ui/needless_collect.rs:93:20
102108
|
103109
LL | foo((0..10).collect::<Vec<_>>());
104110
| ^^^^^^^^^^^^^^^^^^^^ help: remove this call
105111

106112
error: avoid using `collect()` when not needed
107-
--> tests/ui/needless_collect.rs:91:49
113+
--> tests/ui/needless_collect.rs:95:49
108114
|
109115
LL | bar((0..10).collect::<Vec<_>>(), (0..10).collect::<Vec<_>>());
110116
| ^^^^^^^^^^^^^^^^^^^^ help: remove this call
111117

112118
error: avoid using `collect()` when not needed
113-
--> tests/ui/needless_collect.rs:93:37
119+
--> tests/ui/needless_collect.rs:97:37
114120
|
115121
LL | baz((0..10), (), ('a'..='z').collect::<Vec<_>>())
116122
| ^^^^^^^^^^^^^^^^^^^^ help: remove this call
117123

118-
error: aborting due to 19 previous errors
124+
error: aborting due to 20 previous errors
119125

0 commit comments

Comments
 (0)