Skip to content

Commit 33798bb

Browse files
committed
Improve needless_collect output
1 parent 4356a8f commit 33798bb

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

clippy_lints/src/loops/needless_collect.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_hir::intravisit::{walk_block, walk_expr, NestedVisitorMap, Visitor};
1010
use rustc_hir::{Block, Expr, ExprKind, GenericArg, HirId, Local, Pat, PatKind, QPath, StmtKind};
1111
use rustc_lint::LateContext;
1212
use rustc_middle::hir::map::Map;
13-
use rustc_span::source_map::Span;
1413
use rustc_span::symbol::{sym, Ident};
14+
use rustc_span::{MultiSpan, Span};
1515

1616
const NEEDLESS_COLLECT_MSG: &str = "avoid using `collect()` when not needed";
1717

@@ -65,7 +65,7 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
6565
Local { pat: Pat { hir_id: pat_id, kind: PatKind::Binding(_, _, ident, .. ), .. },
6666
init: Some(ref init_expr), .. }
6767
) = stmt.kind;
68-
if let ExprKind::MethodCall(ref method_name, _, &[ref iter_source], ..) = init_expr.kind;
68+
if let ExprKind::MethodCall(ref method_name, collect_span, &[ref iter_source], ..) = init_expr.kind;
6969
if method_name.ident.name == sym!(collect) && is_trait_method(cx, &init_expr, sym::Iterator);
7070
if let Some(ref generic_args) = method_name.args;
7171
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
@@ -74,7 +74,7 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
7474
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
7575
match_type(cx, ty, &paths::LINKED_LIST);
7676
if let Some(iter_calls) = detect_iter_and_into_iters(block, *ident);
77-
if iter_calls.len() == 1;
77+
if let [iter_call] = &*iter_calls;
7878
then {
7979
let mut used_count_visitor = UsedCountVisitor {
8080
cx,
@@ -87,11 +87,12 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
8787
}
8888

8989
// Suggest replacing iter_call with iter_replacement, and removing stmt
90-
let iter_call = &iter_calls[0];
90+
let mut span = MultiSpan::from_span(collect_span);
91+
span.push_span_label(iter_call.span, "the iterator could be used here instead".into());
9192
span_lint_and_then(
9293
cx,
9394
super::NEEDLESS_COLLECT,
94-
stmt.span.until(iter_call.span),
95+
span,
9596
NEEDLESS_COLLECT_MSG,
9697
|diag| {
9798
let iter_replacement = format!("{}{}", Sugg::hir(cx, iter_source, ".."), iter_call.get_iter_method(cx));

clippy_utils/src/diagnostics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ pub fn span_lint_and_note<'a, T: LintContext>(
133133
///
134134
/// If you need to customize your lint output a lot, use this function.
135135
/// If you change the signature, remember to update the internal lint `CollapsibleCalls`
136-
pub fn span_lint_and_then<'a, T: LintContext, F>(cx: &'a T, lint: &'static Lint, sp: Span, msg: &str, f: F)
136+
pub fn span_lint_and_then<C, S, F>(cx: &C, lint: &'static Lint, sp: S, msg: &str, f: F)
137137
where
138-
F: for<'b> FnOnce(&mut DiagnosticBuilder<'b>),
138+
C: LintContext,
139+
S: Into<MultiSpan>,
140+
F: FnOnce(&mut DiagnosticBuilder<'_>),
139141
{
140142
cx.struct_span_lint(lint, sp, |diag| {
141143
let mut diag = diag.build(msg);

tests/ui/needless_collect_indirect.stderr

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
error: avoid using `collect()` when not needed
2-
--> $DIR/needless_collect_indirect.rs:5:5
2+
--> $DIR/needless_collect_indirect.rs:5:39
33
|
4-
LL | / let indirect_iter = sample.iter().collect::<Vec<_>>();
5-
LL | | indirect_iter.into_iter().map(|x| (x, x + 1)).collect::<HashMap<_, _>>();
6-
| |____^
4+
LL | let indirect_iter = sample.iter().collect::<Vec<_>>();
5+
| ^^^^^^^
6+
LL | indirect_iter.into_iter().map(|x| (x, x + 1)).collect::<HashMap<_, _>>();
7+
| ------------------------- the iterator could be used here instead
78
|
89
= note: `-D clippy::needless-collect` implied by `-D warnings`
910
help: use the original Iterator instead of collecting it and then producing a new one
@@ -13,11 +14,12 @@ LL | sample.iter().map(|x| (x, x + 1)).collect::<HashMap<_, _>>();
1314
|
1415

1516
error: avoid using `collect()` when not needed
16-
--> $DIR/needless_collect_indirect.rs:7:5
17+
--> $DIR/needless_collect_indirect.rs:7:38
1718
|
18-
LL | / let indirect_len = sample.iter().collect::<VecDeque<_>>();
19-
LL | | indirect_len.len();
20-
| |____^
19+
LL | let indirect_len = sample.iter().collect::<VecDeque<_>>();
20+
| ^^^^^^^
21+
LL | indirect_len.len();
22+
| ------------------ the iterator could be used here instead
2123
|
2224
help: take the original Iterator's count instead of collecting it and finding the length
2325
|
@@ -26,11 +28,12 @@ LL | sample.iter().count();
2628
|
2729

2830
error: avoid using `collect()` when not needed
29-
--> $DIR/needless_collect_indirect.rs:9:5
31+
--> $DIR/needless_collect_indirect.rs:9:40
3032
|
31-
LL | / let indirect_empty = sample.iter().collect::<VecDeque<_>>();
32-
LL | | indirect_empty.is_empty();
33-
| |____^
33+
LL | let indirect_empty = sample.iter().collect::<VecDeque<_>>();
34+
| ^^^^^^^
35+
LL | indirect_empty.is_empty();
36+
| ------------------------- the iterator could be used here instead
3437
|
3538
help: check if the original Iterator has anything instead of collecting it and seeing if it's empty
3639
|
@@ -39,11 +42,12 @@ LL | sample.iter().next().is_none();
3942
|
4043

4144
error: avoid using `collect()` when not needed
42-
--> $DIR/needless_collect_indirect.rs:11:5
45+
--> $DIR/needless_collect_indirect.rs:11:43
4346
|
44-
LL | / let indirect_contains = sample.iter().collect::<VecDeque<_>>();
45-
LL | | indirect_contains.contains(&&5);
46-
| |____^
47+
LL | let indirect_contains = sample.iter().collect::<VecDeque<_>>();
48+
| ^^^^^^^
49+
LL | indirect_contains.contains(&&5);
50+
| ------------------------------- the iterator could be used here instead
4751
|
4852
help: check if the original Iterator contains an element instead of collecting then checking
4953
|
@@ -52,11 +56,12 @@ LL | sample.iter().any(|x| x == &5);
5256
|
5357

5458
error: avoid using `collect()` when not needed
55-
--> $DIR/needless_collect_indirect.rs:23:5
59+
--> $DIR/needless_collect_indirect.rs:23:48
5660
|
57-
LL | / let non_copy_contains = sample.into_iter().collect::<Vec<_>>();
58-
LL | | non_copy_contains.contains(&a);
59-
| |____^
61+
LL | let non_copy_contains = sample.into_iter().collect::<Vec<_>>();
62+
| ^^^^^^^
63+
LL | non_copy_contains.contains(&a);
64+
| ------------------------------ the iterator could be used here instead
6065
|
6166
help: check if the original Iterator contains an element instead of collecting then checking
6267
|

0 commit comments

Comments
 (0)