]> git.lizzy.rs Git - rust.git/blob - tests/ui/unnecessary_fold.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / unnecessary_fold.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
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.
9
10 /// Calls which should trigger the `UNNECESSARY_FOLD` lint
11 fn unnecessary_fold() {
12     // Can be replaced by .any
13     let _ = (0..3).fold(false, |acc, x| acc || x > 2);
14     // Can be replaced by .all
15     let _ = (0..3).fold(true, |acc, x| acc && x > 2);
16     // Can be replaced by .sum
17     let _ = (0..3).fold(0, |acc, x| acc + x);
18     // Can be replaced by .product
19     let _ = (0..3).fold(1, |acc, x| acc * x);
20 }
21
22 /// Should trigger the `UNNECESSARY_FOLD` lint, with an error span including exactly `.fold(...)`
23 fn unnecessary_fold_span_for_multi_element_chain() {
24     let _ = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
25 }
26
27 /// Calls which should not trigger the `UNNECESSARY_FOLD` lint
28 fn unnecessary_fold_should_ignore() {
29     let _ = (0..3).fold(true, |acc, x| acc || x > 2);
30     let _ = (0..3).fold(false, |acc, x| acc && x > 2);
31     let _ = (0..3).fold(1, |acc, x| acc + x);
32     let _ = (0..3).fold(0, |acc, x| acc * x);
33     let _ = (0..3).fold(0, |acc, x| 1 + acc + x);
34
35     // We only match against an accumulator on the left
36     // hand side. We could lint for .sum and .product when
37     // it's on the right, but don't for now (and this wouldn't
38     // be valid if we extended the lint to cover arbitrary numeric
39     // types).
40     let _ = (0..3).fold(false, |acc, x| x > 2 || acc);
41     let _ = (0..3).fold(true, |acc, x| x > 2 && acc);
42     let _ = (0..3).fold(0, |acc, x| x + acc);
43     let _ = (0..3).fold(1, |acc, x| x * acc);
44
45     let _ = [(0..2), (0..3)].iter().fold(0, |a, b| a + b.len());
46     let _ = [(0..2), (0..3)].iter().fold(1, |a, b| a * b.len());
47 }
48
49 fn main() {}