Skip to content

UI Test Cleanup: Extract match_ref_pats tests #4280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions tests/ui/match_ref_pats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#![warn(clippy::match_ref_pats)]

fn ref_pats() {
{
let v = &Some(0);
match v {
&Some(v) => println!("{:?}", v),
&None => println!("none"),
}
match v {
// This doesn't trigger; we have a different pattern.
&Some(v) => println!("some"),
other => println!("other"),
}
}
let tup = &(1, 2);
match tup {
&(v, 1) => println!("{}", v),
_ => println!("none"),
}
// Special case: using `&` both in expr and pats.
let w = Some(0);
match &w {
&Some(v) => println!("{:?}", v),
&None => println!("none"),
}
// False positive: only wildcard pattern.
let w = Some(0);
match w {
_ => println!("none"),
}

let a = &Some(0);
if let &None = a {
println!("none");
}

let b = Some(0);
if let &None = &b {
println!("none");
}
}

mod ice_3719 {
macro_rules! foo_variant(
($idx:expr) => (Foo::get($idx).unwrap())
);

enum Foo {
A,
B,
}

impl Foo {
fn get(idx: u8) -> Option<&'static Self> {
match idx {
0 => Some(&Foo::A),
1 => Some(&Foo::B),
_ => None,
}
}
}

fn ice_3719() {
// ICE #3719
match foo_variant!(0) {
&Foo::A => println!("A"),
_ => println!("Wild"),
}
}
}

fn main() {}
86 changes: 86 additions & 0 deletions tests/ui/match_ref_pats.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
error: you don't need to add `&` to all patterns
--> $DIR/match_ref_pats.rs:6:9
|
LL | / match v {
LL | | &Some(v) => println!("{:?}", v),
LL | | &None => println!("none"),
LL | | }
| |_________^
|
= note: `-D clippy::match-ref-pats` implied by `-D warnings`
help: instead of prefixing all patterns with `&`, you can dereference the expression
|
LL | match *v {
LL | Some(v) => println!("{:?}", v),
LL | None => println!("none"),
|

error: you don't need to add `&` to all patterns
--> $DIR/match_ref_pats.rs:17:5
|
LL | / match tup {
LL | | &(v, 1) => println!("{}", v),
LL | | _ => println!("none"),
LL | | }
| |_____^
help: instead of prefixing all patterns with `&`, you can dereference the expression
|
LL | match *tup {
LL | (v, 1) => println!("{}", v),
|

error: you don't need to add `&` to both the expression and the patterns
--> $DIR/match_ref_pats.rs:23:5
|
LL | / match &w {
LL | | &Some(v) => println!("{:?}", v),
LL | | &None => println!("none"),
LL | | }
| |_____^
help: try
|
LL | match w {
LL | Some(v) => println!("{:?}", v),
LL | None => println!("none"),
|

error: you don't need to add `&` to all patterns
--> $DIR/match_ref_pats.rs:34:5
|
LL | / if let &None = a {
LL | | println!("none");
LL | | }
| |_____^
help: instead of prefixing all patterns with `&`, you can dereference the expression
|
LL | if let None = *a {
| ^^^^ ^^

error: you don't need to add `&` to both the expression and the patterns
--> $DIR/match_ref_pats.rs:39:5
|
LL | / if let &None = &b {
LL | | println!("none");
LL | | }
| |_____^
help: try
|
LL | if let None = b {
| ^^^^ ^

error: you don't need to add `&` to all patterns
--> $DIR/match_ref_pats.rs:66:9
|
LL | / match foo_variant!(0) {
LL | | &Foo::A => println!("A"),
LL | | _ => println!("Wild"),
LL | | }
| |_________^
help: instead of prefixing all patterns with `&`, you can dereference the expression
|
LL | match *foo_variant!(0) {
LL | Foo::A => println!("A"),
|

error: aborting due to 6 previous errors

68 changes: 1 addition & 67 deletions tests/ui/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,6 @@

fn dummy() {}

fn ref_pats() {
{
let v = &Some(0);
match v {
&Some(v) => println!("{:?}", v),
&None => println!("none"),
}
match v {
// This doesn't trigger; we have a different pattern.
&Some(v) => println!("some"),
other => println!("other"),
}
}
let tup = &(1, 2);
match tup {
&(v, 1) => println!("{}", v),
_ => println!("none"),
}
// Special case: using `&` both in expr and pats.
let w = Some(0);
match &w {
&Some(v) => println!("{:?}", v),
&None => println!("none"),
}
// False positive: only wildcard pattern.
let w = Some(0);
match w {
_ => println!("none"),
}

let a = &Some(0);
if let &None = a {
println!("none");
}

let b = Some(0);
if let &None = &b {
println!("none");
}
}

fn match_wild_err_arm() {
let x: Result<i32, &str> = Ok(3);

Expand Down Expand Up @@ -136,29 +95,4 @@ fn match_wild_err_arm() {
}
}

macro_rules! foo_variant(
($idx:expr) => (Foo::get($idx).unwrap())
);

enum Foo {
A,
B,
}

impl Foo {
fn get(idx: u8) -> Option<&'static Self> {
match idx {
0 => Some(&Foo::A),
1 => Some(&Foo::B),
_ => None,
}
}
}

fn main() {
// ICE #3719
match foo_variant!(0) {
&Foo::A => println!("A"),
_ => println!("Wild"),
}
}
fn main() {}
Loading