Skip to content

Commit 0626674

Browse files
committed
Auto merge of #4280 - phansch:uitests_matches, r=flip1995
UI Test Cleanup: Extract match_ref_pats tests changelog: none cc #2038
2 parents 5dfb5ad + 022b987 commit 0626674

File tree

4 files changed

+191
-182
lines changed

4 files changed

+191
-182
lines changed

tests/ui/match_ref_pats.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#![warn(clippy::match_ref_pats)]
2+
3+
fn ref_pats() {
4+
{
5+
let v = &Some(0);
6+
match v {
7+
&Some(v) => println!("{:?}", v),
8+
&None => println!("none"),
9+
}
10+
match v {
11+
// This doesn't trigger; we have a different pattern.
12+
&Some(v) => println!("some"),
13+
other => println!("other"),
14+
}
15+
}
16+
let tup = &(1, 2);
17+
match tup {
18+
&(v, 1) => println!("{}", v),
19+
_ => println!("none"),
20+
}
21+
// Special case: using `&` both in expr and pats.
22+
let w = Some(0);
23+
match &w {
24+
&Some(v) => println!("{:?}", v),
25+
&None => println!("none"),
26+
}
27+
// False positive: only wildcard pattern.
28+
let w = Some(0);
29+
match w {
30+
_ => println!("none"),
31+
}
32+
33+
let a = &Some(0);
34+
if let &None = a {
35+
println!("none");
36+
}
37+
38+
let b = Some(0);
39+
if let &None = &b {
40+
println!("none");
41+
}
42+
}
43+
44+
mod ice_3719 {
45+
macro_rules! foo_variant(
46+
($idx:expr) => (Foo::get($idx).unwrap())
47+
);
48+
49+
enum Foo {
50+
A,
51+
B,
52+
}
53+
54+
impl Foo {
55+
fn get(idx: u8) -> Option<&'static Self> {
56+
match idx {
57+
0 => Some(&Foo::A),
58+
1 => Some(&Foo::B),
59+
_ => None,
60+
}
61+
}
62+
}
63+
64+
fn ice_3719() {
65+
// ICE #3719
66+
match foo_variant!(0) {
67+
&Foo::A => println!("A"),
68+
_ => println!("Wild"),
69+
}
70+
}
71+
}
72+
73+
fn main() {}

tests/ui/match_ref_pats.stderr

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
error: you don't need to add `&` to all patterns
2+
--> $DIR/match_ref_pats.rs:6:9
3+
|
4+
LL | / match v {
5+
LL | | &Some(v) => println!("{:?}", v),
6+
LL | | &None => println!("none"),
7+
LL | | }
8+
| |_________^
9+
|
10+
= note: `-D clippy::match-ref-pats` implied by `-D warnings`
11+
help: instead of prefixing all patterns with `&`, you can dereference the expression
12+
|
13+
LL | match *v {
14+
LL | Some(v) => println!("{:?}", v),
15+
LL | None => println!("none"),
16+
|
17+
18+
error: you don't need to add `&` to all patterns
19+
--> $DIR/match_ref_pats.rs:17:5
20+
|
21+
LL | / match tup {
22+
LL | | &(v, 1) => println!("{}", v),
23+
LL | | _ => println!("none"),
24+
LL | | }
25+
| |_____^
26+
help: instead of prefixing all patterns with `&`, you can dereference the expression
27+
|
28+
LL | match *tup {
29+
LL | (v, 1) => println!("{}", v),
30+
|
31+
32+
error: you don't need to add `&` to both the expression and the patterns
33+
--> $DIR/match_ref_pats.rs:23:5
34+
|
35+
LL | / match &w {
36+
LL | | &Some(v) => println!("{:?}", v),
37+
LL | | &None => println!("none"),
38+
LL | | }
39+
| |_____^
40+
help: try
41+
|
42+
LL | match w {
43+
LL | Some(v) => println!("{:?}", v),
44+
LL | None => println!("none"),
45+
|
46+
47+
error: you don't need to add `&` to all patterns
48+
--> $DIR/match_ref_pats.rs:34:5
49+
|
50+
LL | / if let &None = a {
51+
LL | | println!("none");
52+
LL | | }
53+
| |_____^
54+
help: instead of prefixing all patterns with `&`, you can dereference the expression
55+
|
56+
LL | if let None = *a {
57+
| ^^^^ ^^
58+
59+
error: you don't need to add `&` to both the expression and the patterns
60+
--> $DIR/match_ref_pats.rs:39:5
61+
|
62+
LL | / if let &None = &b {
63+
LL | | println!("none");
64+
LL | | }
65+
| |_____^
66+
help: try
67+
|
68+
LL | if let None = b {
69+
| ^^^^ ^
70+
71+
error: you don't need to add `&` to all patterns
72+
--> $DIR/match_ref_pats.rs:66:9
73+
|
74+
LL | / match foo_variant!(0) {
75+
LL | | &Foo::A => println!("A"),
76+
LL | | _ => println!("Wild"),
77+
LL | | }
78+
| |_________^
79+
help: instead of prefixing all patterns with `&`, you can dereference the expression
80+
|
81+
LL | match *foo_variant!(0) {
82+
LL | Foo::A => println!("A"),
83+
|
84+
85+
error: aborting due to 6 previous errors
86+

tests/ui/matches.rs

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,6 @@
55

66
fn dummy() {}
77

8-
fn ref_pats() {
9-
{
10-
let v = &Some(0);
11-
match v {
12-
&Some(v) => println!("{:?}", v),
13-
&None => println!("none"),
14-
}
15-
match v {
16-
// This doesn't trigger; we have a different pattern.
17-
&Some(v) => println!("some"),
18-
other => println!("other"),
19-
}
20-
}
21-
let tup = &(1, 2);
22-
match tup {
23-
&(v, 1) => println!("{}", v),
24-
_ => println!("none"),
25-
}
26-
// Special case: using `&` both in expr and pats.
27-
let w = Some(0);
28-
match &w {
29-
&Some(v) => println!("{:?}", v),
30-
&None => println!("none"),
31-
}
32-
// False positive: only wildcard pattern.
33-
let w = Some(0);
34-
match w {
35-
_ => println!("none"),
36-
}
37-
38-
let a = &Some(0);
39-
if let &None = a {
40-
println!("none");
41-
}
42-
43-
let b = Some(0);
44-
if let &None = &b {
45-
println!("none");
46-
}
47-
}
48-
498
fn match_wild_err_arm() {
509
let x: Result<i32, &str> = Ok(3);
5110

@@ -136,29 +95,4 @@ fn match_wild_err_arm() {
13695
}
13796
}
13897

139-
macro_rules! foo_variant(
140-
($idx:expr) => (Foo::get($idx).unwrap())
141-
);
142-
143-
enum Foo {
144-
A,
145-
B,
146-
}
147-
148-
impl Foo {
149-
fn get(idx: u8) -> Option<&'static Self> {
150-
match idx {
151-
0 => Some(&Foo::A),
152-
1 => Some(&Foo::B),
153-
_ => None,
154-
}
155-
}
156-
}
157-
158-
fn main() {
159-
// ICE #3719
160-
match foo_variant!(0) {
161-
&Foo::A => println!("A"),
162-
_ => println!("Wild"),
163-
}
164-
}
98+
fn main() {}

0 commit comments

Comments
 (0)