Skip to content

Commit a936b6d

Browse files
authored
Merge pull request #2664 from phansch/move_unnecessary_fold_ui_tests
Move unnecessary_fold UI tests to separate file
2 parents 8ec61a6 + dfde407 commit a936b6d

File tree

4 files changed

+77
-74
lines changed

4 files changed

+77
-74
lines changed

tests/ui/methods.rs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -385,45 +385,6 @@ fn iter_skip_next() {
385385
let _ = foo.filter().skip(42).next();
386386
}
387387

388-
/// Calls which should trigger the `UNNECESSARY_FOLD` lint
389-
fn unnecessary_fold() {
390-
// Can be replaced by .any
391-
let _ = (0..3).fold(false, |acc, x| acc || x > 2);
392-
// Can be replaced by .all
393-
let _ = (0..3).fold(true, |acc, x| acc && x > 2);
394-
// Can be replaced by .sum
395-
let _ = (0..3).fold(0, |acc, x| acc + x);
396-
// Can be replaced by .product
397-
let _ = (0..3).fold(1, |acc, x| acc * x);
398-
}
399-
400-
/// Should trigger the `UNNECESSARY_FOLD` lint, with an error span including exactly `.fold(...)`
401-
fn unnecessary_fold_span_for_multi_element_chain() {
402-
let _ = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
403-
}
404-
405-
/// Calls which should not trigger the `UNNECESSARY_FOLD` lint
406-
fn unnecessary_fold_should_ignore() {
407-
let _ = (0..3).fold(true, |acc, x| acc || x > 2);
408-
let _ = (0..3).fold(false, |acc, x| acc && x > 2);
409-
let _ = (0..3).fold(1, |acc, x| acc + x);
410-
let _ = (0..3).fold(0, |acc, x| acc * x);
411-
let _ = (0..3).fold(0, |acc, x| 1 + acc + x);
412-
413-
// We only match against an accumulator on the left
414-
// hand side. We could lint for .sum and .product when
415-
// it's on the right, but don't for now (and this wouldn't
416-
// be valid if we extended the lint to cover arbitrary numeric
417-
// types).
418-
let _ = (0..3).fold(false, |acc, x| x > 2 || acc);
419-
let _ = (0..3).fold(true, |acc, x| x > 2 && acc);
420-
let _ = (0..3).fold(0, |acc, x| x + acc);
421-
let _ = (0..3).fold(1, |acc, x| x * acc);
422-
423-
let _ = [(0..2), (0..3)].iter().fold(0, |a, b| a + b.len());
424-
let _ = [(0..2), (0..3)].iter().fold(1, |a, b| a * b.len());
425-
}
426-
427388
#[allow(similar_names)]
428389
fn main() {
429390
let opt = Some(0);

tests/ui/methods.stderr

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -493,45 +493,13 @@ error: called `skip(x).next()` on an iterator. This is more succinctly expressed
493493
382 | let _ = &some_vec[..].iter().skip(3).next();
494494
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
495495

496-
error: this `.fold` can be written more succinctly using another method
497-
--> $DIR/methods.rs:391:19
498-
|
499-
391 | let _ = (0..3).fold(false, |acc, x| acc || x > 2);
500-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
501-
|
502-
= note: `-D unnecessary-fold` implied by `-D warnings`
503-
504-
error: this `.fold` can be written more succinctly using another method
505-
--> $DIR/methods.rs:393:19
506-
|
507-
393 | let _ = (0..3).fold(true, |acc, x| acc && x > 2);
508-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.all(|x| x > 2)`
509-
510-
error: this `.fold` can be written more succinctly using another method
511-
--> $DIR/methods.rs:395:19
512-
|
513-
395 | let _ = (0..3).fold(0, |acc, x| acc + x);
514-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.sum()`
515-
516-
error: this `.fold` can be written more succinctly using another method
517-
--> $DIR/methods.rs:397:19
518-
|
519-
397 | let _ = (0..3).fold(1, |acc, x| acc * x);
520-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.product()`
521-
522-
error: this `.fold` can be written more succinctly using another method
523-
--> $DIR/methods.rs:402:34
524-
|
525-
402 | let _ = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
526-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
527-
528496
error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
529-
--> $DIR/methods.rs:430:13
497+
--> $DIR/methods.rs:391:13
530498
|
531-
430 | let _ = opt.unwrap();
499+
391 | let _ = opt.unwrap();
532500
| ^^^^^^^^^^^^
533501
|
534502
= note: `-D option-unwrap-used` implied by `-D warnings`
535503

536-
error: aborting due to 71 previous errors
504+
error: aborting due to 66 previous errors
537505

tests/ui/unnecessary_fold.rs

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

tests/ui/unnecessary_fold.stderr

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: this `.fold` can be written more succinctly using another method
2+
--> $DIR/unnecessary_fold.rs:4:19
3+
|
4+
4 | let _ = (0..3).fold(false, |acc, x| acc || x > 2);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
6+
|
7+
= note: `-D unnecessary-fold` implied by `-D warnings`
8+
9+
error: this `.fold` can be written more succinctly using another method
10+
--> $DIR/unnecessary_fold.rs:6:19
11+
|
12+
6 | let _ = (0..3).fold(true, |acc, x| acc && x > 2);
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.all(|x| x > 2)`
14+
15+
error: this `.fold` can be written more succinctly using another method
16+
--> $DIR/unnecessary_fold.rs:8:19
17+
|
18+
8 | let _ = (0..3).fold(0, |acc, x| acc + x);
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.sum()`
20+
21+
error: this `.fold` can be written more succinctly using another method
22+
--> $DIR/unnecessary_fold.rs:10:19
23+
|
24+
10 | let _ = (0..3).fold(1, |acc, x| acc * x);
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.product()`
26+
27+
error: this `.fold` can be written more succinctly using another method
28+
--> $DIR/unnecessary_fold.rs:15:34
29+
|
30+
15 | let _ = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
32+
33+
error: aborting due to 5 previous errors
34+

0 commit comments

Comments
 (0)