fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
5.2 KiB248 linesraw
1#![warn(clippy::needless_match)]
2#![allow(clippy::manual_map)]
3#![allow(dead_code)]
4
5#[derive(Clone, Copy)]
6enum Simple {
7    A,
8    B,
9    C,
10    D,
11}
12
13fn useless_match() {
14    let i = 10;
15    let _: i32 = i;
16    let s = "test";
17    let _: &str = s;
18}
19
20fn custom_type_match() {
21    let se = Simple::A;
22    let _: Simple = se;
23    // Don't trigger
24    let _: Simple = match se {
25        Simple::A => Simple::A,
26        Simple::B => Simple::B,
27        _ => Simple::C,
28    };
29    // Mingled, don't trigger
30    let _: Simple = match se {
31        Simple::A => Simple::B,
32        Simple::B => Simple::C,
33        Simple::C => Simple::D,
34        Simple::D => Simple::A,
35    };
36}
37
38fn option_match(x: Option<i32>) {
39    let _: Option<i32> = x;
40    // Don't trigger, this is the case for manual_map_option
41    let _: Option<i32> = match x {
42        Some(a) => Some(-a),
43        None => None,
44    };
45}
46
47fn func_ret_err<T>(err: T) -> Result<i32, T> {
48    Err(err)
49}
50
51fn result_match() {
52    let _: Result<i32, i32> = Ok(1);
53    let _: Result<i32, i32> = func_ret_err(0_i32);
54    // as ref, don't trigger
55    let res = &func_ret_err(0_i32);
56    let _: Result<&i32, &i32> = match *res {
57        Ok(ref x) => Ok(x),
58        Err(ref x) => Err(x),
59    };
60}
61
62fn if_let_option() {
63    let _ = Some(1);
64
65    fn do_something() {}
66
67    // Don't trigger
68    let _ = if let Some(a) = Some(1) {
69        Some(a)
70    } else {
71        do_something();
72        None
73    };
74
75    // Don't trigger
76    let _ = if let Some(a) = Some(1) {
77        do_something();
78        Some(a)
79    } else {
80        None
81    };
82
83    // Don't trigger
84    let _ = if let Some(a) = Some(1) { Some(a) } else { Some(2) };
85}
86
87fn if_let_option_result() -> Result<(), ()> {
88    fn f(x: i32) -> Result<Option<i32>, ()> {
89        Ok(Some(x))
90    }
91    // Don't trigger
92    let _ = if let Some(v) = f(1)? { Some(v) } else { f(2)? };
93    Ok(())
94}
95
96fn if_let_result() {
97    let x: Result<i32, i32> = Ok(1);
98    let _: Result<i32, i32> = x;
99    let _: Result<i32, i32> = x;
100    // Input type mismatch, don't trigger
101    #[allow(clippy::question_mark)]
102    let _: Result<i32, i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
103}
104
105fn if_let_custom_enum(x: Simple) {
106    let _: Simple = x;
107
108    // Don't trigger
109    let _: Simple = if let Simple::A = x {
110        Simple::A
111    } else if true {
112        Simple::B
113    } else {
114        x
115    };
116}
117
118mod issue8542 {
119    #[derive(Clone, Copy)]
120    enum E {
121        VariantA(u8, u8),
122        VariantB(u8, bool),
123    }
124
125    enum Complex {
126        A(u8),
127        B(u8, bool),
128        C(u8, i32, f64),
129        D(E, bool),
130    }
131
132    fn match_test() {
133        let ce = Complex::B(8, false);
134        let aa = 0_u8;
135        let bb = false;
136
137        let _: Complex = ce;
138
139        // Don't trigger
140        let _: Complex = match ce {
141            Complex::A(_) => Complex::A(aa),
142            Complex::B(_, b) => Complex::B(aa, b),
143            Complex::C(_, b, _) => Complex::C(aa, b, 64_f64),
144            Complex::D(e, b) => Complex::D(e, b),
145        };
146
147        // Don't trigger
148        let _: Complex = match ce {
149            Complex::A(a) => Complex::A(a),
150            Complex::B(a, _) => Complex::B(a, bb),
151            Complex::C(a, _, _) => Complex::C(a, 32_i32, 64_f64),
152            _ => ce,
153        };
154    }
155}
156
157/// Lint triggered when type coercions happen.
158/// Do NOT trigger on any of these.
159mod issue8551 {
160    trait Trait {}
161    struct Struct;
162    impl Trait for Struct {}
163
164    fn optmap(s: Option<&Struct>) -> Option<&dyn Trait> {
165        match s {
166            Some(s) => Some(s),
167            None => None,
168        }
169    }
170
171    fn lint_tests() {
172        let option: Option<&Struct> = None;
173        let _: Option<&dyn Trait> = match option {
174            Some(s) => Some(s),
175            None => None,
176        };
177
178        let _: Option<&dyn Trait> = if true {
179            match option {
180                Some(s) => Some(s),
181                None => None,
182            }
183        } else {
184            None
185        };
186
187        let result: Result<&Struct, i32> = Err(0);
188        let _: Result<&dyn Trait, i32> = match result {
189            Ok(s) => Ok(s),
190            Err(e) => Err(e),
191        };
192
193        let _: Option<&dyn Trait> = if let Some(s) = option { Some(s) } else { None };
194    }
195}
196
197trait Tr {
198    fn as_mut(&mut self) -> Result<&mut i32, &mut i32>;
199}
200impl Tr for Result<i32, i32> {
201    fn as_mut(&mut self) -> Result<&mut i32, &mut i32> {
202        match self {
203            Ok(x) => Ok(x),
204            Err(e) => Err(e),
205        }
206    }
207}
208
209mod issue9084 {
210    fn wildcard_if() {
211        let mut some_bool = true;
212        let e = Some(1);
213
214        // should lint
215        let _ = e;
216
217        // should lint
218        let _ = e;
219
220        // should not lint
221        let _ = match e {
222            _ if some_bool => e,
223            _ => Some(2),
224        };
225
226        // should not lint
227        let _ = match e {
228            Some(i) => Some(i + 1),
229            _ if some_bool => e,
230            _ => e,
231        };
232
233        // should not lint (guard has side effects)
234        let _ = match e {
235            Some(i) => Some(i),
236            _ if {
237                some_bool = false;
238                some_bool
239            } =>
240            {
241                e
242            },
243            _ => e,
244        };
245    }
246}
247
248fn main() {}