]> git.lizzy.rs Git - rust.git/blob - tests/ui/unnecessary_fold.rs
map_unit_fn: rename tests to fixable
[rust.git] / tests / ui / unnecessary_fold.rs
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).fold(false, |acc, x| acc || x > 2);
9     // Can be replaced by .all
10     let _ = (0..3).fold(true, |acc, x| acc && x > 2);
11     // Can be replaced by .sum
12     let _: i32 = (0..3).fold(0, |acc, x| acc + x);
13     // Can be replaced by .product
14     let _: i32 = (0..3).fold(1, |acc, x| acc * x);
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).fold(false, |acc, x| acc || 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 /// Should lint only the line containing the fold
45 fn unnecessary_fold_over_multiple_lines() {
46     let _ = (0..3)
47         .map(|x| x + 1)
48         .filter(|x| x % 2 == 0)
49         .fold(false, |acc, x| acc || x > 2);
50 }
51
52 fn main() {}