summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/manual_filter.fixed
blob: 3553291b87df6731aca2e71f905ed63eef21cda7 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// run-rustfix

#![warn(clippy::manual_filter)]
#![allow(unused_variables, dead_code)]

fn main() {
    Some(0).filter(|&x| x <= 0);

    Some(1).filter(|&x| x <= 0);

    Some(2).filter(|&x| x <= 0);

    Some(3).filter(|&x| x > 0);

    let y = Some(4);
    y.filter(|&x| x <= 0);

    Some(5).filter(|&x| x > 0);

    Some(6).as_ref().filter(|&x| x > &0);

    let external_cond = true;
    Some(String::new()).filter(|x| external_cond);

    Some(7).filter(|&x| external_cond);

    Some(8).filter(|&x| x != 0);

    Some(9).filter(|&x| x > 10 && x < 100);

    const fn f1() {
        // Don't lint, `.filter` is not const
        match Some(10) {
            Some(x) => {
                if x > 10 && x < 100 {
                    Some(x)
                } else {
                    None
                }
            },
            None => None,
        };
    }

    #[allow(clippy::blocks_in_if_conditions)]
    Some(11).filter(|&x| {
                println!("foo");
                x > 10 && x < 100
            });

    match Some(12) {
        // Don't Lint, statement is lost by `.filter`
        Some(x) => {
            if x > 10 && x < 100 {
                println!("foo");
                Some(x)
            } else {
                None
            }
        },
        None => None,
    };

    match Some(13) {
        // Don't Lint, because of `None => Some(1)`
        Some(x) => {
            if x > 10 && x < 100 {
                println!("foo");
                Some(x)
            } else {
                None
            }
        },
        None => Some(1),
    };

    unsafe fn f(x: u32) -> bool {
        true
    }
    let _ = Some(14).filter(|&x| unsafe { f(x) });
    let _ = Some(15).filter(|&x| unsafe { f(x) });

    #[allow(clippy::redundant_pattern_matching)]
    if let Some(_) = Some(16) {
        Some(16)
    } else { Some(16).filter(|&x| x % 2 == 0) };

    match Some((17, 17)) {
        // Not linted for now could be
        Some((x, y)) => {
            if y != x {
                Some((x, y))
            } else {
                None
            }
        },
        None => None,
    };

    struct NamedTuple {
        pub x: u8,
        pub y: (i32, u32),
    }

    match Some(NamedTuple {
        // Not linted for now could be
        x: 17,
        y: (18, 19),
    }) {
        Some(NamedTuple { x, y }) => {
            if y.1 != x as u32 {
                Some(NamedTuple { x, y })
            } else {
                None
            }
        },
        None => None,
    };
}