]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/match_ref_pats.rs
Rollup merge of #90487 - NoraCodes:nora/how-to-read-rustdoc, r=jyn514
[rust.git] / src / tools / clippy / tests / ui / match_ref_pats.rs
1 #![warn(clippy::match_ref_pats)]
2 #![allow(clippy::equatable_if_let, clippy::enum_variant_names)]
3
4 fn ref_pats() {
5     {
6         let v = &Some(0);
7         match v {
8             &Some(v) => println!("{:?}", v),
9             &None => println!("none"),
10         }
11         match v {
12             // This doesn't trigger; we have a different pattern.
13             &Some(v) => println!("some"),
14             other => println!("other"),
15         }
16     }
17     let tup = &(1, 2);
18     match tup {
19         &(v, 1) => println!("{}", v),
20         _ => println!("none"),
21     }
22     // Special case: using `&` both in expr and pats.
23     let w = Some(0);
24     match &w {
25         &Some(v) => println!("{:?}", v),
26         &None => println!("none"),
27     }
28     // False positive: only wildcard pattern.
29     let w = Some(0);
30     #[allow(clippy::match_single_binding)]
31     match w {
32         _ => println!("none"),
33     }
34
35     let a = &Some(0);
36     if let &None = a {
37         println!("none");
38     }
39
40     let b = Some(0);
41     if let &None = &b {
42         println!("none");
43     }
44 }
45
46 mod ice_3719 {
47     macro_rules! foo_variant(
48         ($idx:expr) => (Foo::get($idx).unwrap())
49     );
50
51     enum Foo {
52         A,
53         B,
54     }
55
56     impl Foo {
57         fn get(idx: u8) -> Option<&'static Self> {
58             match idx {
59                 0 => Some(&Foo::A),
60                 1 => Some(&Foo::B),
61                 _ => None,
62             }
63         }
64     }
65
66     fn ice_3719() {
67         // ICE #3719
68         match foo_variant!(0) {
69             &Foo::A => println!("A"),
70             _ => println!("Wild"),
71         }
72     }
73 }
74
75 mod issue_7740 {
76     macro_rules! foobar_variant(
77         ($idx:expr) => (FooBar::get($idx).unwrap())
78     );
79
80     enum FooBar {
81         Foo,
82         Bar,
83         FooBar,
84         BarFoo,
85     }
86
87     impl FooBar {
88         fn get(idx: u8) -> Option<&'static Self> {
89             match idx {
90                 0 => Some(&FooBar::Foo),
91                 1 => Some(&FooBar::Bar),
92                 2 => Some(&FooBar::FooBar),
93                 3 => Some(&FooBar::BarFoo),
94                 _ => None,
95             }
96         }
97     }
98
99     fn issue_7740() {
100         // Issue #7740
101         match foobar_variant!(0) {
102             &FooBar::Foo => println!("Foo"),
103             &FooBar::Bar => println!("Bar"),
104             &FooBar::FooBar => println!("FooBar"),
105             _ => println!("Wild"),
106         }
107
108         // This shouldn't trigger
109         if let &FooBar::BarFoo = foobar_variant!(3) {
110             println!("BarFoo");
111         } else {
112             println!("Wild");
113         }
114     }
115 }
116
117 fn main() {}