1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 /// Calls which should trigger the `UNNECESSARY_FOLD` lint
12 fn unnecessary_fold() {
13 // Can be replaced by .any
14 let _ = (0..3).fold(false, |acc, x| acc || x > 2);
15 // Can be replaced by .all
16 let _ = (0..3).fold(true, |acc, x| acc && x > 2);
17 // Can be replaced by .sum
18 let _ = (0..3).fold(0, |acc, x| acc + x);
19 // Can be replaced by .product
20 let _ = (0..3).fold(1, |acc, x| acc * x);
23 /// Should trigger the `UNNECESSARY_FOLD` lint, with an error span including exactly `.fold(...)`
24 fn unnecessary_fold_span_for_multi_element_chain() {
25 let _ = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
28 /// Calls which should not trigger the `UNNECESSARY_FOLD` lint
29 fn unnecessary_fold_should_ignore() {
30 let _ = (0..3).fold(true, |acc, x| acc || x > 2);
31 let _ = (0..3).fold(false, |acc, x| acc && x > 2);
32 let _ = (0..3).fold(1, |acc, x| acc + x);
33 let _ = (0..3).fold(0, |acc, x| acc * x);
34 let _ = (0..3).fold(0, |acc, x| 1 + acc + x);
36 // We only match against an accumulator on the left
37 // hand side. We could lint for .sum and .product when
38 // it's on the right, but don't for now (and this wouldn't
39 // be valid if we extended the lint to cover arbitrary numeric
41 let _ = (0..3).fold(false, |acc, x| x > 2 || acc);
42 let _ = (0..3).fold(true, |acc, x| x > 2 && acc);
43 let _ = (0..3).fold(0, |acc, x| x + acc);
44 let _ = (0..3).fold(1, |acc, x| x * acc);
46 let _ = [(0..2), (0..3)].iter().fold(0, |a, b| a + b.len());
47 let _ = [(0..2), (0..3)].iter().fold(1, |a, b| a * b.len());
49 // Because `any` takes the iterator as a mutable reference,
50 // it cannot be used in a pattern guard, and we must use `fold`.
52 _ if (0..3).fold(false, |acc, x| acc || x > 2) => {}