2023-04-23 11:03:09 +00:00
|
|
|
//@run-rustfix
|
2020-07-14 12:59:59 +00:00
|
|
|
|
|
|
|
#![warn(clippy::match_like_matches_macro)]
|
2022-11-21 19:34:47 +00:00
|
|
|
#![allow(
|
|
|
|
unreachable_patterns,
|
|
|
|
dead_code,
|
|
|
|
clippy::equatable_if_let,
|
|
|
|
clippy::needless_borrowed_reference
|
|
|
|
)]
|
2020-07-14 12:59:59 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = Some(5);
|
|
|
|
|
|
|
|
// Lint
|
|
|
|
let _y = matches!(x, Some(0));
|
|
|
|
|
|
|
|
// Lint
|
|
|
|
let _w = matches!(x, Some(_));
|
|
|
|
|
|
|
|
// Turn into is_none
|
|
|
|
let _z = x.is_none();
|
|
|
|
|
|
|
|
// Lint
|
|
|
|
let _zz = !matches!(x, Some(r) if r == 0);
|
|
|
|
|
|
|
|
// Lint
|
|
|
|
let _zzz = matches!(x, Some(5));
|
|
|
|
|
|
|
|
// No lint
|
|
|
|
let _a = match x {
|
|
|
|
Some(_) => false,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
// No lint
|
|
|
|
let _ab = match x {
|
|
|
|
Some(0) => false,
|
|
|
|
_ => true,
|
|
|
|
None => false,
|
|
|
|
};
|
2020-10-28 22:36:07 +00:00
|
|
|
|
|
|
|
enum E {
|
|
|
|
A(u32),
|
|
|
|
B(i32),
|
|
|
|
C,
|
|
|
|
D,
|
2020-12-29 22:15:53 +00:00
|
|
|
}
|
2020-10-28 22:36:07 +00:00
|
|
|
let x = E::A(2);
|
|
|
|
{
|
|
|
|
// lint
|
|
|
|
let _ans = matches!(x, E::A(_) | E::B(_));
|
|
|
|
}
|
2022-07-18 07:39:37 +00:00
|
|
|
{
|
|
|
|
// lint
|
|
|
|
// skip rustfmt to prevent removing block for first pattern
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let _ans = matches!(x, E::A(_) | E::B(_));
|
|
|
|
}
|
2020-10-28 22:36:07 +00:00
|
|
|
{
|
|
|
|
// lint
|
|
|
|
let _ans = !matches!(x, E::B(_) | E::C);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// no lint
|
|
|
|
let _ans = match x {
|
|
|
|
E::A(_) => false,
|
|
|
|
E::B(_) => false,
|
|
|
|
E::C => true,
|
|
|
|
_ => true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// no lint
|
|
|
|
let _ans = match x {
|
|
|
|
E::A(_) => true,
|
|
|
|
E::B(_) => false,
|
|
|
|
E::C => false,
|
|
|
|
_ => true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// no lint
|
|
|
|
let _ans = match x {
|
|
|
|
E::A(a) if a < 10 => false,
|
|
|
|
E::B(a) if a < 10 => false,
|
|
|
|
_ => true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// no lint
|
|
|
|
let _ans = match x {
|
|
|
|
E::A(_) => false,
|
|
|
|
E::B(a) if a < 10 => false,
|
|
|
|
_ => true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// no lint
|
|
|
|
let _ans = match x {
|
|
|
|
E::A(a) => a == 10,
|
|
|
|
E::B(_) => false,
|
|
|
|
_ => true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// no lint
|
|
|
|
let _ans = match x {
|
|
|
|
E::A(_) => false,
|
|
|
|
E::B(_) => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
}
|
2021-01-30 17:06:34 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// should print "z" in suggestion (#6503)
|
|
|
|
let z = &Some(3);
|
|
|
|
let _z = matches!(z, Some(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// this could also print "z" in suggestion..?
|
|
|
|
let z = Some(3);
|
|
|
|
let _z = matches!(&z, Some(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
enum AnEnum {
|
|
|
|
X,
|
|
|
|
Y,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(_x: AnEnum) {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let z = AnEnum::X;
|
|
|
|
// we can't remove the reference here!
|
|
|
|
let _ = matches!(&z, AnEnum::X);
|
|
|
|
foo(z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
struct S(i32);
|
|
|
|
|
|
|
|
fn fun(_val: Option<S>) {}
|
|
|
|
let val = Some(S(42));
|
|
|
|
// we need the reference here because later val is consumed by fun()
|
|
|
|
let _res = matches!(&val, &Some(ref _a));
|
|
|
|
fun(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
struct S(i32);
|
|
|
|
|
|
|
|
fn fun(_val: Option<S>) {}
|
|
|
|
let val = Some(S(42));
|
|
|
|
let _res = matches!(&val, &Some(ref _a));
|
|
|
|
fun(val);
|
|
|
|
}
|
2022-02-26 13:26:21 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
enum E {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = match E::A {
|
|
|
|
E::B => true,
|
|
|
|
#[cfg(feature = "foo")]
|
|
|
|
E::A => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
}
|
2022-08-31 13:24:45 +00:00
|
|
|
|
|
|
|
let x = ' ';
|
|
|
|
// ignore if match block contains comment
|
|
|
|
let _line_comments = match x {
|
|
|
|
// numbers are bad!
|
|
|
|
'1' | '2' | '3' => true,
|
|
|
|
// spaces are very important to be true.
|
|
|
|
' ' => true,
|
|
|
|
// as are dots
|
|
|
|
'.' => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let _block_comments = match x {
|
|
|
|
/* numbers are bad!
|
|
|
|
*/
|
|
|
|
'1' | '2' | '3' => true,
|
|
|
|
/* spaces are very important to be true.
|
|
|
|
*/
|
|
|
|
' ' => true,
|
|
|
|
/* as are dots
|
|
|
|
*/
|
|
|
|
'.' => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
2020-07-14 12:59:59 +00:00
|
|
|
}
|
2022-10-23 13:18:45 +00:00
|
|
|
|
2022-12-01 17:29:38 +00:00
|
|
|
#[clippy::msrv = "1.41"]
|
2022-10-23 13:18:45 +00:00
|
|
|
fn msrv_1_41() {
|
|
|
|
let _y = match Some(5) {
|
|
|
|
Some(0) => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-01 17:29:38 +00:00
|
|
|
#[clippy::msrv = "1.42"]
|
2022-10-23 13:18:45 +00:00
|
|
|
fn msrv_1_42() {
|
|
|
|
let _y = matches!(Some(5), Some(0));
|
|
|
|
}
|