Skip to content

Commit a849483

Browse files
committed
Fix formatting and dogfood fallout
1 parent c86f410 commit a849483

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

clippy_lints/src/loops.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,7 +2359,10 @@ impl<'a, 'tcx> Visitor<'tcx> for VarCollectorVisitor<'a, 'tcx> {
23592359
const NEEDLESS_COLLECT_MSG: &str = "avoid using `collect()` when not needed";
23602360

23612361
fn check_needless_collect<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) {
2362-
// Check for direct, immediate usage
2362+
check_needless_collect_direct_usage(expr, cx);
2363+
check_needless_collect_indirect_usage(expr, cx);
2364+
}
2365+
fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) {
23632366
if_chain! {
23642367
if let ExprKind::MethodCall(ref method, _, ref args, _) = expr.kind;
23652368
if let ExprKind::MethodCall(ref chain_method, _, _, _) = args[0].kind;
@@ -2425,7 +2428,9 @@ fn check_needless_collect<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) {
24252428
}
24262429
}
24272430
}
2428-
// Check for collecting it and then turning it back into an iterator later
2431+
}
2432+
2433+
fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) {
24292434
if let ExprKind::Block(ref block, _) = expr.kind {
24302435
for ref stmt in block.stmts {
24312436
if_chain! {
@@ -2484,10 +2489,18 @@ impl IterFunction {
24842489
}
24852490
fn get_suggestion_text(&self) -> &'static str {
24862491
match &self.func {
2487-
IterFunctionKind::IntoIter => "Use the original Iterator instead of collecting it and then producing a new one",
2488-
IterFunctionKind::Len => "Take the original Iterator's count instead of collecting it and finding the length",
2489-
IterFunctionKind::IsEmpty => "Check if the original Iterator has anything instead of collecting it and seeing if it's empty",
2490-
IterFunctionKind::Contains(_) => "Check if the original Iterator contains an element instead of collecting then checking",
2492+
IterFunctionKind::IntoIter => {
2493+
"Use the original Iterator instead of collecting it and then producing a new one"
2494+
},
2495+
IterFunctionKind::Len => {
2496+
"Take the original Iterator's count instead of collecting it and finding the length"
2497+
},
2498+
IterFunctionKind::IsEmpty => {
2499+
"Check if the original Iterator has anything instead of collecting it and seeing if it's empty"
2500+
},
2501+
IterFunctionKind::Contains(_) => {
2502+
"Check if the original Iterator contains an element instead of collecting then checking"
2503+
},
24912504
}
24922505
}
24932506
}
@@ -2505,6 +2518,8 @@ struct IterFunctionVisitor {
25052518
}
25062519
impl<'tcx> Visitor<'tcx> for IterFunctionVisitor {
25072520
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
2521+
// TODO Check if the target identifier is being used in something other
2522+
// than a function call
25082523
if_chain! {
25092524
if let ExprKind::MethodCall(method_name, _, ref args, _) = &expr.kind;
25102525
if let Some(Expr { kind: ExprKind::Path(QPath::Resolved(_, ref path)), .. }) = args.get(0);
@@ -2515,10 +2530,18 @@ impl<'tcx> Visitor<'tcx> for IterFunctionVisitor {
25152530
let is_empty = sym!(is_empty);
25162531
let contains = sym!(contains);
25172532
match method_name.ident.name {
2518-
name if name == into_iter => self.uses.push(IterFunction { func: IterFunctionKind::IntoIter, span: expr.span }),
2519-
name if name == len => self.uses.push(IterFunction { func: IterFunctionKind::Len, span: expr.span }),
2520-
name if name == is_empty => self.uses.push(IterFunction { func: IterFunctionKind::IsEmpty, span: expr.span }),
2521-
name if name == contains => self.uses.push(IterFunction { func: IterFunctionKind::Contains(args[1].span), span: expr.span }),
2533+
name if name == into_iter => self.uses.push(
2534+
IterFunction { func: IterFunctionKind::IntoIter, span: expr.span }
2535+
),
2536+
name if name == len => self.uses.push(
2537+
IterFunction { func: IterFunctionKind::Len, span: expr.span }
2538+
),
2539+
name if name == is_empty => self.uses.push(
2540+
IterFunction { func: IterFunctionKind::IsEmpty, span: expr.span }
2541+
),
2542+
name if name == contains => self.uses.push(
2543+
IterFunction { func: IterFunctionKind::Contains(args[1].span), span: expr.span }
2544+
),
25222545
_ => self.seen_other = true,
25232546
}
25242547
}

tests/ui/needless_collect_indirect.fixed

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
// run-rustfix
22

33
#[allow(unused)]
4-
54
use std::collections::{HashMap, VecDeque};
65

76
fn main() {
87
let sample = [1; 5];
98
let indirect_iter = sample.iter().collect::<Vec<_>>();
10-
indirect_iter
11-
.into_iter()
12-
.map(|x| (x, x + 1))
13-
.collect::<HashMap<_, _>>();
9+
indirect_iter.into_iter().map(|x| (x, x + 1)).collect::<HashMap<_, _>>();
1410
let indirect_len = sample.iter().collect::<VecDeque<_>>();
1511
indirect_len.len();
1612
let indirect_empty = sample.iter().collect::<VecDeque<_>>();

tests/ui/needless_collect_indirect.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
// run-rustfix
22

33
#[allow(unused)]
4-
54
use std::collections::{HashMap, VecDeque};
65

76
fn main() {
87
let sample = [1; 5];
98
let indirect_iter = sample.iter().collect::<Vec<_>>();
10-
indirect_iter
11-
.into_iter()
12-
.map(|x| (x, x + 1))
13-
.collect::<HashMap<_, _>>();
9+
indirect_iter.into_iter().map(|x| (x, x + 1)).collect::<HashMap<_, _>>();
1410
let indirect_len = sample.iter().collect::<VecDeque<_>>();
1511
indirect_len.len();
1612
let indirect_empty = sample.iter().collect::<VecDeque<_>>();

tests/ui/needless_collect_indirect.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error: avoid using `collect()` when not needed
2-
--> $DIR/needless_collect_indirect.rs:9:5
2+
--> $DIR/needless_collect_indirect.rs:8:5
33
|
44
LL | / let indirect_iter = sample.iter().collect::<Vec<_>>();
5-
LL | | indirect_iter
5+
LL | | indirect_iter.into_iter().map(|x| (x, x + 1)).collect::<HashMap<_, _>>();
66
| |____^
77
|
88
= note: `-D clippy::needless-collect` implied by `-D warnings`
99
help: Use the original Iterator instead of collecting it and then producing a new one
1010
|
1111
LL |
12-
LL | sample.iter()
12+
LL | sample.iter().map(|x| (x, x + 1)).collect::<HashMap<_, _>>();
1313
|
1414

1515
error: avoid using `collect()` when not needed
16-
--> $DIR/needless_collect_indirect.rs:14:5
16+
--> $DIR/needless_collect_indirect.rs:10:5
1717
|
1818
LL | / let indirect_len = sample.iter().collect::<VecDeque<_>>();
1919
LL | | indirect_len.len();
@@ -26,7 +26,7 @@ LL | sample.iter().count();
2626
|
2727

2828
error: avoid using `collect()` when not needed
29-
--> $DIR/needless_collect_indirect.rs:16:5
29+
--> $DIR/needless_collect_indirect.rs:12:5
3030
|
3131
LL | / let indirect_empty = sample.iter().collect::<VecDeque<_>>();
3232
LL | | indirect_empty.is_empty();
@@ -39,7 +39,7 @@ LL | sample.iter().next().is_none();
3939
|
4040

4141
error: avoid using `collect()` when not needed
42-
--> $DIR/needless_collect_indirect.rs:18:5
42+
--> $DIR/needless_collect_indirect.rs:14:5
4343
|
4444
LL | / let indirect_contains = sample.iter().collect::<VecDeque<_>>();
4545
LL | | indirect_contains.contains(&&5);

0 commit comments

Comments
 (0)