Skip to content

Commit 1080f6c

Browse files
committed
Adds additional tests for lint
1 parent fc32425 commit 1080f6c

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

tests/ui/match_ref_pats.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,46 @@ mod ice_3719 {
7272
}
7373
}
7474

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+
75117
fn main() {}

tests/ui/match_ref_pats.stderr

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,24 @@ error: redundant pattern matching, consider using `is_none()`
4545
LL | if let &None = &b {
4646
| -------^^^^^----- help: try this: `if b.is_none()`
4747

48-
error: aborting due to 4 previous errors
48+
error: you don't need to add `&` to all patterns
49+
--> $DIR/match_ref_pats.rs:101:9
50+
|
51+
LL | / match foobar_variant!(0) {
52+
LL | | &FooBar::Foo => println!("Foo"),
53+
LL | | &FooBar::Bar => println!("Bar"),
54+
LL | | &FooBar::FooBar => println!("FooBar"),
55+
LL | | _ => println!("Wild"),
56+
LL | | }
57+
| |_________^
58+
|
59+
help: instead of prefixing all patterns with `&`, you can dereference the expression
60+
|
61+
LL ~ match *foobar_variant!(0) {
62+
LL ~ FooBar::Foo => println!("Foo"),
63+
LL ~ FooBar::Bar => println!("Bar"),
64+
LL ~ FooBar::FooBar => println!("FooBar"),
65+
|
66+
67+
error: aborting due to 5 previous errors
4968

0 commit comments

Comments
 (0)