2018-07-28 15:34:52 +00:00
|
|
|
#![warn(clippy::single_match)]
|
2018-04-05 19:18:38 +00:00
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
fn dummy() {}
|
2018-04-05 19:18:38 +00:00
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
fn single_match() {
|
2018-04-05 19:18:38 +00:00
|
|
|
let x = Some(1u8);
|
|
|
|
|
|
|
|
match x {
|
2018-12-09 22:26:16 +00:00
|
|
|
Some(y) => {
|
|
|
|
println!("{:?}", y);
|
|
|
|
},
|
|
|
|
_ => (),
|
2018-04-05 19:18:38 +00:00
|
|
|
};
|
|
|
|
|
2018-10-27 12:45:02 +00:00
|
|
|
let x = Some(1u8);
|
|
|
|
match x {
|
|
|
|
// Note the missing block braces.
|
|
|
|
// We suggest `if let Some(y) = x { .. }` because the macro
|
|
|
|
// is expanded before we can do anything.
|
|
|
|
Some(y) => println!("{:?}", y),
|
2018-12-09 22:26:16 +00:00
|
|
|
_ => (),
|
2018-10-27 12:45:02 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
let z = (1u8, 1u8);
|
2018-04-05 19:18:38 +00:00
|
|
|
match z {
|
2019-06-01 05:54:47 +00:00
|
|
|
(2..=3, 7..=9) => dummy(),
|
2018-12-09 22:26:16 +00:00
|
|
|
_ => {},
|
2018-04-05 19:18:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Not linted (pattern guards used)
|
|
|
|
match x {
|
|
|
|
Some(y) if y == 0 => println!("{:?}", y),
|
2018-12-09 22:26:16 +00:00
|
|
|
_ => (),
|
2018-04-05 19:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not linted (no block with statements in the single arm)
|
|
|
|
match z {
|
2019-06-01 05:54:47 +00:00
|
|
|
(2..=3, 7..=9) => println!("{:?}", z),
|
2018-04-05 19:18:38 +00:00
|
|
|
_ => println!("nope"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
enum Foo {
|
|
|
|
Bar,
|
|
|
|
Baz(u8),
|
|
|
|
}
|
2018-04-05 19:18:38 +00:00
|
|
|
use std::borrow::Cow;
|
2018-12-09 22:26:16 +00:00
|
|
|
use Foo::*;
|
2018-04-05 19:18:38 +00:00
|
|
|
|
|
|
|
fn single_match_know_enum() {
|
|
|
|
let x = Some(1u8);
|
2018-12-09 22:26:16 +00:00
|
|
|
let y: Result<_, i8> = Ok(1i8);
|
2018-04-05 19:18:38 +00:00
|
|
|
|
|
|
|
match x {
|
|
|
|
Some(y) => dummy(),
|
2018-12-09 22:26:16 +00:00
|
|
|
None => (),
|
2018-04-05 19:18:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match y {
|
|
|
|
Ok(y) => dummy(),
|
2018-12-09 22:26:16 +00:00
|
|
|
Err(..) => (),
|
2018-04-05 19:18:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let c = Cow::Borrowed("");
|
|
|
|
|
|
|
|
match c {
|
|
|
|
Cow::Borrowed(..) => dummy(),
|
|
|
|
Cow::Owned(..) => (),
|
|
|
|
};
|
|
|
|
|
|
|
|
let z = Foo::Bar;
|
|
|
|
// no warning
|
|
|
|
match z {
|
|
|
|
Bar => println!("42"),
|
|
|
|
Baz(_) => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match z {
|
|
|
|
Baz(_) => println!("42"),
|
|
|
|
Bar => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 17:06:34 +00:00
|
|
|
// issue #173
|
|
|
|
fn if_suggestion() {
|
|
|
|
let x = "test";
|
|
|
|
match x {
|
|
|
|
"test" => println!(),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
enum Foo {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C(u32),
|
|
|
|
}
|
|
|
|
|
|
|
|
let x = Foo::A;
|
|
|
|
match x {
|
|
|
|
Foo::A => println!(),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
const FOO_C: Foo = Foo::C(0);
|
|
|
|
match x {
|
|
|
|
FOO_C => println!(),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match &&x {
|
|
|
|
Foo::A => println!(),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
let x = &x;
|
|
|
|
match &x {
|
|
|
|
Foo::A => println!(),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Bar {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
}
|
|
|
|
impl PartialEq for Bar {
|
|
|
|
fn eq(&self, rhs: &Self) -> bool {
|
|
|
|
matches!((self, rhs), (Self::A, Self::A) | (Self::B, Self::B))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Eq for Bar {}
|
|
|
|
|
|
|
|
let x = Bar::A;
|
|
|
|
match x {
|
|
|
|
Bar::A => println!(),
|
|
|
|
_ => (),
|
|
|
|
}
|
2021-04-22 09:31:13 +00:00
|
|
|
|
|
|
|
// issue #7038
|
|
|
|
struct X;
|
|
|
|
let x = Some(X);
|
|
|
|
match x {
|
|
|
|
None => println!(),
|
|
|
|
_ => (),
|
|
|
|
};
|
2021-01-30 17:06:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 10:19:27 +00:00
|
|
|
macro_rules! single_match {
|
|
|
|
($num:literal) => {
|
|
|
|
match $num {
|
|
|
|
15 => println!("15"),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-31 10:43:51 +00:00
|
|
|
fn main() {
|
|
|
|
single_match!(5);
|
|
|
|
}
|