]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/manual_filter.fixed
Rollup merge of #105801 - zertosh:path_mut_os_str_doc_test, r=dtolnay
[rust.git] / src / tools / clippy / tests / ui / manual_filter.fixed
1 // run-rustfix
2
3 #![warn(clippy::manual_filter)]
4 #![allow(unused_variables, dead_code)]
5
6 fn main() {
7     Some(0).filter(|&x| x <= 0);
8
9     Some(1).filter(|&x| x <= 0);
10
11     Some(2).filter(|&x| x <= 0);
12
13     Some(3).filter(|&x| x > 0);
14
15     let y = Some(4);
16     y.filter(|&x| x <= 0);
17
18     Some(5).filter(|&x| x > 0);
19
20     Some(6).as_ref().filter(|&x| x > &0);
21
22     let external_cond = true;
23     Some(String::new()).filter(|x| external_cond);
24
25     Some(7).filter(|&x| external_cond);
26
27     Some(8).filter(|&x| x != 0);
28
29     Some(9).filter(|&x| x > 10 && x < 100);
30
31     const fn f1() {
32         // Don't lint, `.filter` is not const
33         match Some(10) {
34             Some(x) => {
35                 if x > 10 && x < 100 {
36                     Some(x)
37                 } else {
38                     None
39                 }
40             },
41             None => None,
42         };
43     }
44
45     #[allow(clippy::blocks_in_if_conditions)]
46     Some(11).filter(|&x| {
47                 println!("foo");
48                 x > 10 && x < 100
49             });
50
51     match Some(12) {
52         // Don't Lint, statement is lost by `.filter`
53         Some(x) => {
54             if x > 10 && x < 100 {
55                 println!("foo");
56                 Some(x)
57             } else {
58                 None
59             }
60         },
61         None => None,
62     };
63
64     match Some(13) {
65         // Don't Lint, because of `None => Some(1)`
66         Some(x) => {
67             if x > 10 && x < 100 {
68                 println!("foo");
69                 Some(x)
70             } else {
71                 None
72             }
73         },
74         None => Some(1),
75     };
76
77     unsafe fn f(x: u32) -> bool {
78         true
79     }
80     let _ = Some(14).filter(|&x| unsafe { f(x) });
81     let _ = Some(15).filter(|&x| unsafe { f(x) });
82
83     #[allow(clippy::redundant_pattern_matching)]
84     if let Some(_) = Some(16) {
85         Some(16)
86     } else { Some(16).filter(|&x| x % 2 == 0) };
87
88     match Some((17, 17)) {
89         // Not linted for now could be
90         Some((x, y)) => {
91             if y != x {
92                 Some((x, y))
93             } else {
94                 None
95             }
96         },
97         None => None,
98     };
99
100     struct NamedTuple {
101         pub x: u8,
102         pub y: (i32, u32),
103     }
104
105     match Some(NamedTuple {
106         // Not linted for now could be
107         x: 17,
108         y: (18, 19),
109     }) {
110         Some(NamedTuple { x, y }) => {
111             if y.1 != x as u32 {
112                 Some(NamedTuple { x, y })
113             } else {
114                 None
115             }
116         },
117         None => None,
118     };
119 }