Skip to content

Commit 455c07b

Browse files
committedJan 29, 2024
Auto merge of #12210 - GuillaumeGomez:add-regression-test-2371, r=blyxyas
Add regression ui test for #2371 Fixes #2371. #2371 seems to already be handled correctly in the lint. This PR adds a ui regression test so we can close it. r? `@blyxyas` changelog: Add regression ui test for #2371
2 parents 1156375 + ce8b4b6 commit 455c07b

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed
 

Diff for: ‎clippy_lints/src/methods/unnecessary_fold.rs

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ fn check_fold_with_op(
9999
cx,
100100
UNNECESSARY_FOLD,
101101
fold_span.with_hi(expr.span.hi()),
102-
// TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
103102
"this `.fold` can be written more succinctly using another method",
104103
"try",
105104
sugg,

Diff for: ‎tests/ui/unnecessary_fold.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#![allow(dead_code)]
22

3+
fn is_any(acc: bool, x: usize) -> bool {
4+
acc || x > 2
5+
}
6+
37
/// Calls which should trigger the `UNNECESSARY_FOLD` lint
48
fn unnecessary_fold() {
59
// Can be replaced by .any
610
let _ = (0..3).any(|x| x > 2);
11+
// Can be replaced by .any (checking suggestion)
12+
let _ = (0..3).fold(false, is_any);
713
// Can be replaced by .all
814
let _ = (0..3).all(|x| x > 2);
915
// Can be replaced by .sum

Diff for: ‎tests/ui/unnecessary_fold.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#![allow(dead_code)]
22

3+
fn is_any(acc: bool, x: usize) -> bool {
4+
acc || x > 2
5+
}
6+
37
/// Calls which should trigger the `UNNECESSARY_FOLD` lint
48
fn unnecessary_fold() {
59
// Can be replaced by .any
610
let _ = (0..3).fold(false, |acc, x| acc || x > 2);
11+
// Can be replaced by .any (checking suggestion)
12+
let _ = (0..3).fold(false, |acc, x| is_any(acc, x));
713
// Can be replaced by .all
814
let _ = (0..3).fold(true, |acc, x| acc && x > 2);
915
// Can be replaced by .sum

Diff for: ‎tests/ui/unnecessary_fold.stderr

+25-16
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,104 @@
11
error: this `.fold` can be written more succinctly using another method
2-
--> $DIR/unnecessary_fold.rs:6:20
2+
--> $DIR/unnecessary_fold.rs:10:20
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
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_fold)]`
99

10+
error: redundant closure
11+
--> $DIR/unnecessary_fold.rs:12:32
12+
|
13+
LL | let _ = (0..3).fold(false, |acc, x| is_any(acc, x));
14+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `is_any`
15+
|
16+
= note: `-D clippy::redundant-closure` implied by `-D warnings`
17+
= help: to override `-D warnings` add `#[allow(clippy::redundant_closure)]`
18+
1019
error: this `.fold` can be written more succinctly using another method
11-
--> $DIR/unnecessary_fold.rs:8:20
20+
--> $DIR/unnecessary_fold.rs:14:20
1221
|
1322
LL | let _ = (0..3).fold(true, |acc, x| acc && x > 2);
1423
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `all(|x| x > 2)`
1524

1625
error: this `.fold` can be written more succinctly using another method
17-
--> $DIR/unnecessary_fold.rs:10:25
26+
--> $DIR/unnecessary_fold.rs:16:25
1827
|
1928
LL | let _: i32 = (0..3).fold(0, |acc, x| acc + x);
2029
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `sum()`
2130

2231
error: this `.fold` can be written more succinctly using another method
23-
--> $DIR/unnecessary_fold.rs:12:25
32+
--> $DIR/unnecessary_fold.rs:18:25
2433
|
2534
LL | let _: i32 = (0..3).fold(1, |acc, x| acc * x);
2635
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `product()`
2736

2837
error: this `.fold` can be written more succinctly using another method
29-
--> $DIR/unnecessary_fold.rs:17:41
38+
--> $DIR/unnecessary_fold.rs:23:41
3039
|
3140
LL | let _: bool = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
3241
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `any(|x| x > 2)`
3342

3443
error: this `.fold` can be written more succinctly using another method
35-
--> $DIR/unnecessary_fold.rs:47:10
44+
--> $DIR/unnecessary_fold.rs:53:10
3645
|
3746
LL | .fold(false, |acc, x| acc || x > 2);
3847
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `any(|x| x > 2)`
3948

4049
error: this `.fold` can be written more succinctly using another method
41-
--> $DIR/unnecessary_fold.rs:58:33
50+
--> $DIR/unnecessary_fold.rs:64:33
4251
|
4352
LL | assert_eq!(map.values().fold(0, |x, y| x + y), 0);
4453
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `sum::<i32>()`
4554

4655
error: this `.fold` can be written more succinctly using another method
47-
--> $DIR/unnecessary_fold.rs:61:30
56+
--> $DIR/unnecessary_fold.rs:67:30
4857
|
4958
LL | let _ = map.values().fold(0, |x, y| x + y);
5059
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `sum::<i32>()`
5160

5261
error: this `.fold` can be written more succinctly using another method
53-
--> $DIR/unnecessary_fold.rs:62:30
62+
--> $DIR/unnecessary_fold.rs:68:30
5463
|
5564
LL | let _ = map.values().fold(1, |x, y| x * y);
5665
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `product::<i32>()`
5766

5867
error: this `.fold` can be written more succinctly using another method
59-
--> $DIR/unnecessary_fold.rs:63:35
68+
--> $DIR/unnecessary_fold.rs:69:35
6069
|
6170
LL | let _: i32 = map.values().fold(0, |x, y| x + y);
6271
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `sum()`
6372

6473
error: this `.fold` can be written more succinctly using another method
65-
--> $DIR/unnecessary_fold.rs:64:35
74+
--> $DIR/unnecessary_fold.rs:70:35
6675
|
6776
LL | let _: i32 = map.values().fold(1, |x, y| x * y);
6877
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `product()`
6978

7079
error: this `.fold` can be written more succinctly using another method
71-
--> $DIR/unnecessary_fold.rs:65:31
80+
--> $DIR/unnecessary_fold.rs:71:31
7281
|
7382
LL | anything(map.values().fold(0, |x, y| x + y));
7483
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `sum::<i32>()`
7584

7685
error: this `.fold` can be written more succinctly using another method
77-
--> $DIR/unnecessary_fold.rs:66:31
86+
--> $DIR/unnecessary_fold.rs:72:31
7887
|
7988
LL | anything(map.values().fold(1, |x, y| x * y));
8089
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `product::<i32>()`
8190

8291
error: this `.fold` can be written more succinctly using another method
83-
--> $DIR/unnecessary_fold.rs:67:26
92+
--> $DIR/unnecessary_fold.rs:73:26
8493
|
8594
LL | num(map.values().fold(0, |x, y| x + y));
8695
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `sum()`
8796

8897
error: this `.fold` can be written more succinctly using another method
89-
--> $DIR/unnecessary_fold.rs:68:26
98+
--> $DIR/unnecessary_fold.rs:74:26
9099
|
91100
LL | num(map.values().fold(1, |x, y| x * y));
92101
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `product()`
93102

94-
error: aborting due to 15 previous errors
103+
error: aborting due to 16 previous errors
95104

0 commit comments

Comments
 (0)