summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/unnecessary_fold.rs
blob: 2e6d6ba52eb91dd57189d77c630b796ac80f2b49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#![allow(dead_code)]

/// Calls which should trigger the `UNNECESSARY_FOLD` lint
fn unnecessary_fold() {
    // Can be replaced by .any
    let _ = (0..3).fold(false, |acc, x| acc || x > 2);
    // Can be replaced by .all
    let _ = (0..3).fold(true, |acc, x| acc && x > 2);
    // Can be replaced by .sum
    let _: i32 = (0..3).fold(0, |acc, x| acc + x);
    // Can be replaced by .product
    let _: i32 = (0..3).fold(1, |acc, x| acc * x);
}

/// Should trigger the `UNNECESSARY_FOLD` lint, with an error span including exactly `.fold(...)`
fn unnecessary_fold_span_for_multi_element_chain() {
    let _: bool = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
}

/// Calls which should not trigger the `UNNECESSARY_FOLD` lint
fn unnecessary_fold_should_ignore() {
    let _ = (0..3).fold(true, |acc, x| acc || x > 2);
    let _ = (0..3).fold(false, |acc, x| acc && x > 2);
    let _ = (0..3).fold(1, |acc, x| acc + x);
    let _ = (0..3).fold(0, |acc, x| acc * x);
    let _ = (0..3).fold(0, |acc, x| 1 + acc + x);

    // We only match against an accumulator on the left
    // hand side. We could lint for .sum and .product when
    // it's on the right, but don't for now (and this wouldn't
    // be valid if we extended the lint to cover arbitrary numeric
    // types).
    let _ = (0..3).fold(false, |acc, x| x > 2 || acc);
    let _ = (0..3).fold(true, |acc, x| x > 2 && acc);
    let _ = (0..3).fold(0, |acc, x| x + acc);
    let _ = (0..3).fold(1, |acc, x| x * acc);

    let _ = [(0..2), (0..3)].iter().fold(0, |a, b| a + b.len());
    let _ = [(0..2), (0..3)].iter().fold(1, |a, b| a * b.len());
}

/// Should lint only the line containing the fold
fn unnecessary_fold_over_multiple_lines() {
    let _ = (0..3)
        .map(|x| x + 1)
        .filter(|x| x % 2 == 0)
        .fold(false, |acc, x| acc || x > 2);
}

fn issue10000() {
    use std::collections::HashMap;
    use std::hash::BuildHasher;

    fn anything<T>(_: T) {}
    fn num(_: i32) {}
    fn smoketest_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) {
        map.insert(0, 0);
        assert_eq!(map.values().fold(0, |x, y| x + y), 0);

        // more cases:
        let _ = map.values().fold(0, |x, y| x + y);
        let _ = map.values().fold(1, |x, y| x * y);
        let _: i32 = map.values().fold(0, |x, y| x + y);
        let _: i32 = map.values().fold(1, |x, y| x * y);
        anything(map.values().fold(0, |x, y| x + y));
        anything(map.values().fold(1, |x, y| x * y));
        num(map.values().fold(0, |x, y| x + y));
        num(map.values().fold(1, |x, y| x * y));
    }

    smoketest_map(HashMap::new());
}

fn main() {}