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);
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);
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);
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
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);
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());