Skip to content

Commit 51c0dd4

Browse files
committed
Add run-rustfix to unnecessary_fold
1 parent c325137 commit 51c0dd4

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

tests/ui/unnecessary_fold.fixed

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// run-rustfix
2+
3+
#![allow(dead_code)]
4+
5+
/// Calls which should trigger the `UNNECESSARY_FOLD` lint
6+
fn unnecessary_fold() {
7+
// Can be replaced by .any
8+
let _ = (0..3).any(|x| x > 2);
9+
// Can be replaced by .all
10+
let _ = (0..3).all(|x| x > 2);
11+
// Can be replaced by .sum
12+
let _: i32 = (0..3).sum();
13+
// Can be replaced by .product
14+
let _: i32 = (0..3).product();
15+
}
16+
17+
/// Should trigger the `UNNECESSARY_FOLD` lint, with an error span including exactly `.fold(...)`
18+
fn unnecessary_fold_span_for_multi_element_chain() {
19+
let _: bool = (0..3).map(|x| 2 * x).any(|x| x > 2);
20+
}
21+
22+
/// Calls which should not trigger the `UNNECESSARY_FOLD` lint
23+
fn unnecessary_fold_should_ignore() {
24+
let _ = (0..3).fold(true, |acc, x| acc || x > 2);
25+
let _ = (0..3).fold(false, |acc, x| acc && x > 2);
26+
let _ = (0..3).fold(1, |acc, x| acc + x);
27+
let _ = (0..3).fold(0, |acc, x| acc * x);
28+
let _ = (0..3).fold(0, |acc, x| 1 + acc + x);
29+
30+
// We only match against an accumulator on the left
31+
// hand side. We could lint for .sum and .product when
32+
// it's on the right, but don't for now (and this wouldn't
33+
// be valid if we extended the lint to cover arbitrary numeric
34+
// types).
35+
let _ = (0..3).fold(false, |acc, x| x > 2 || acc);
36+
let _ = (0..3).fold(true, |acc, x| x > 2 && acc);
37+
let _ = (0..3).fold(0, |acc, x| x + acc);
38+
let _ = (0..3).fold(1, |acc, x| x * acc);
39+
40+
let _ = [(0..2), (0..3)].iter().fold(0, |a, b| a + b.len());
41+
let _ = [(0..2), (0..3)].iter().fold(1, |a, b| a * b.len());
42+
}
43+
44+
fn main() {}

tests/ui/unnecessary_fold.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1+
// run-rustfix
2+
3+
#![allow(dead_code)]
4+
15
/// Calls which should trigger the `UNNECESSARY_FOLD` lint
26
fn unnecessary_fold() {
37
// Can be replaced by .any
48
let _ = (0..3).fold(false, |acc, x| acc || x > 2);
59
// Can be replaced by .all
610
let _ = (0..3).fold(true, |acc, x| acc && x > 2);
711
// Can be replaced by .sum
8-
let _ = (0..3).fold(0, |acc, x| acc + x);
12+
let _: i32 = (0..3).fold(0, |acc, x| acc + x);
913
// Can be replaced by .product
10-
let _ = (0..3).fold(1, |acc, x| acc * x);
14+
let _: i32 = (0..3).fold(1, |acc, x| acc * x);
1115
}
1216

1317
/// Should trigger the `UNNECESSARY_FOLD` lint, with an error span including exactly `.fold(...)`
1418
fn unnecessary_fold_span_for_multi_element_chain() {
15-
let _ = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
19+
let _: bool = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
1620
}
1721

1822
/// Calls which should not trigger the `UNNECESSARY_FOLD` lint

tests/ui/unnecessary_fold.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
error: this `.fold` can be written more succinctly using another method
2-
--> $DIR/unnecessary_fold.rs:4:19
2+
--> $DIR/unnecessary_fold.rs:8:19
33
|
44
LL | let _ = (0..3).fold(false, |acc, x| acc || x > 2);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
66
|
77
= note: `-D clippy::unnecessary-fold` implied by `-D warnings`
88

99
error: this `.fold` can be written more succinctly using another method
10-
--> $DIR/unnecessary_fold.rs:6:19
10+
--> $DIR/unnecessary_fold.rs:10:19
1111
|
1212
LL | let _ = (0..3).fold(true, |acc, x| acc && x > 2);
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.all(|x| x > 2)`
1414

1515
error: this `.fold` can be written more succinctly using another method
16-
--> $DIR/unnecessary_fold.rs:8:19
16+
--> $DIR/unnecessary_fold.rs:12:24
1717
|
18-
LL | let _ = (0..3).fold(0, |acc, x| acc + x);
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.sum()`
18+
LL | let _: i32 = (0..3).fold(0, |acc, x| acc + x);
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.sum()`
2020

2121
error: this `.fold` can be written more succinctly using another method
22-
--> $DIR/unnecessary_fold.rs:10:19
22+
--> $DIR/unnecessary_fold.rs:14:24
2323
|
24-
LL | let _ = (0..3).fold(1, |acc, x| acc * x);
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.product()`
24+
LL | let _: i32 = (0..3).fold(1, |acc, x| acc * x);
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.product()`
2626

2727
error: this `.fold` can be written more succinctly using another method
28-
--> $DIR/unnecessary_fold.rs:15:34
28+
--> $DIR/unnecessary_fold.rs:19:40
2929
|
30-
LL | let _ = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
30+
LL | let _: bool = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
3232

3333
error: aborting due to 5 previous errors
3434

0 commit comments

Comments
 (0)