]> git.lizzy.rs Git - rust.git/blob - tests/ui/collapsible_match2.rs
Improved shared_code_in_if_blocks message and added test stderrs
[rust.git] / tests / ui / collapsible_match2.rs
1 #![warn(clippy::collapsible_match)]
2 #![allow(clippy::needless_return, clippy::no_effect, clippy::single_match)]
3
4 fn lint_cases(opt_opt: Option<Option<u32>>, res_opt: Result<Option<u32>, String>) {
5     // if guards on outer match
6     {
7         match res_opt {
8             Ok(val) if make() => match val {
9                 Some(n) => foo(n),
10                 _ => return,
11             },
12             _ => return,
13         }
14         match res_opt {
15             Ok(val) => match val {
16                 Some(n) => foo(n),
17                 _ => return,
18             },
19             _ if make() => return,
20             _ => return,
21         }
22     }
23
24     // macro
25     {
26         macro_rules! mac {
27             ($outer:expr => $pat:pat, $e:expr => $inner_pat:pat, $then:expr) => {
28                 match $outer {
29                     $pat => match $e {
30                         $inner_pat => $then,
31                         _ => return,
32                     },
33                     _ => return,
34                 }
35             };
36         }
37         // Lint this since the patterns are not defined by the macro.
38         // Allows the lint to work on if_chain! for example.
39         // Fixing the lint requires knowledge of the specific macro, but we optimistically assume that
40         // there is still a better way to write this.
41         mac!(res_opt => Ok(val), val => Some(n), foo(n));
42     }
43
44     // deref reference value
45     match Some(&[1]) {
46         Some(s) => match *s {
47             [n] => foo(n),
48             _ => (),
49         },
50         _ => (),
51     }
52
53     // ref pattern and deref
54     match Some(&[1]) {
55         Some(ref s) => match &*s {
56             [n] => foo(n),
57             _ => (),
58         },
59         _ => (),
60     }
61 }
62
63 fn no_lint() {
64     // deref inner value (cannot pattern match with Vec)
65     match Some(vec![1]) {
66         Some(s) => match *s {
67             [n] => foo(n),
68             _ => (),
69         },
70         _ => (),
71     }
72 }
73
74 fn make<T>() -> T {
75     unimplemented!()
76 }
77
78 fn foo<T, U>(t: T) -> U {
79     unimplemented!()
80 }
81
82 fn main() {}