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