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