2018-07-28 10:34:52 -05:00
|
|
|
#![warn(clippy::single_match)]
|
2023-06-10 06:43:30 -05:00
|
|
|
#![allow(
|
|
|
|
unused,
|
|
|
|
clippy::uninlined_format_args,
|
|
|
|
clippy::needless_if,
|
2023-06-14 13:51:31 -05:00
|
|
|
clippy::redundant_guards,
|
2024-03-08 15:04:17 -06:00
|
|
|
clippy::redundant_pattern_matching,
|
|
|
|
clippy::manual_unwrap_or_default
|
2023-06-10 06:43:30 -05:00
|
|
|
)]
|
2018-12-09 16:26:16 -06:00
|
|
|
fn dummy() {}
|
2018-04-05 14:18:38 -05:00
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
fn single_match() {
|
2018-04-05 14:18:38 -05:00
|
|
|
let x = Some(1u8);
|
|
|
|
|
|
|
|
match x {
|
2018-12-09 16:26:16 -06:00
|
|
|
Some(y) => {
|
|
|
|
println!("{:?}", y);
|
|
|
|
},
|
|
|
|
_ => (),
|
2018-04-05 14:18:38 -05:00
|
|
|
};
|
|
|
|
|
2018-10-27 07:45:02 -05: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 16:26:16 -06:00
|
|
|
_ => (),
|
2018-10-27 07:45:02 -05:00
|
|
|
}
|
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
let z = (1u8, 1u8);
|
2018-04-05 14:18:38 -05:00
|
|
|
match z {
|
2019-06-01 00:54:47 -05:00
|
|
|
(2..=3, 7..=9) => dummy(),
|
2018-12-09 16:26:16 -06:00
|
|
|
_ => {},
|
2018-04-05 14:18:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Not linted (pattern guards used)
|
|
|
|
match x {
|
|
|
|
Some(y) if y == 0 => println!("{:?}", y),
|
2018-12-09 16:26:16 -06:00
|
|
|
_ => (),
|
2018-04-05 14:18:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not linted (no block with statements in the single arm)
|
|
|
|
match z {
|
2019-06-01 00:54:47 -05:00
|
|
|
(2..=3, 7..=9) => println!("{:?}", z),
|
2018-04-05 14:18:38 -05:00
|
|
|
_ => println!("nope"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
enum Foo {
|
|
|
|
Bar,
|
|
|
|
Baz(u8),
|
|
|
|
}
|
|
|
|
use Foo::*;
|
2024-09-22 13:42:10 -05:00
|
|
|
use std::borrow::Cow;
|
2018-04-05 14:18:38 -05:00
|
|
|
|
|
|
|
fn single_match_know_enum() {
|
|
|
|
let x = Some(1u8);
|
2018-12-09 16:26:16 -06:00
|
|
|
let y: Result<_, i8> = Ok(1i8);
|
2018-04-05 14:18:38 -05:00
|
|
|
|
|
|
|
match x {
|
|
|
|
Some(y) => dummy(),
|
2018-12-09 16:26:16 -06:00
|
|
|
None => (),
|
2018-04-05 14:18:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
match y {
|
|
|
|
Ok(y) => dummy(),
|
2018-12-09 16:26:16 -06:00
|
|
|
Err(..) => (),
|
2018-04-05 14:18:38 -05: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-14 13:26:26 -06:00
|
|
|
// issue #173
|
|
|
|
fn if_suggestion() {
|
2021-01-10 22:32:23 -06:00
|
|
|
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!(),
|
|
|
|
_ => (),
|
|
|
|
}
|
2021-01-14 13:26:26 -06:00
|
|
|
|
|
|
|
match &&x {
|
|
|
|
Foo::A => println!(),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
let x = &x;
|
|
|
|
match &x {
|
|
|
|
Foo::A => println!(),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2021-01-10 22:32:23 -06:00
|
|
|
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-15 21:05:37 -05:00
|
|
|
|
|
|
|
// issue #7038
|
|
|
|
struct X;
|
|
|
|
let x = Some(X);
|
|
|
|
match x {
|
|
|
|
None => println!(),
|
|
|
|
_ => (),
|
|
|
|
};
|
2021-01-10 22:32:23 -06:00
|
|
|
}
|
|
|
|
|
2022-01-20 05:50:14 -06:00
|
|
|
// See: issue #8282
|
|
|
|
fn ranges() {
|
|
|
|
enum E {
|
|
|
|
V,
|
|
|
|
}
|
|
|
|
let x = (Some(E::V), Some(42));
|
|
|
|
|
2022-01-20 22:24:07 -06:00
|
|
|
// Don't lint, because the `E` enum can be extended with additional fields later. Thus, the
|
|
|
|
// proposed replacement to `if let Some(E::V)` may hide non-exhaustive warnings that appeared
|
|
|
|
// because of `match` construction.
|
2022-01-20 05:50:14 -06:00
|
|
|
match x {
|
|
|
|
(Some(E::V), _) => {},
|
|
|
|
(None, _) => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// lint
|
|
|
|
match x {
|
|
|
|
(Some(_), _) => {},
|
|
|
|
(None, _) => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// lint
|
|
|
|
match x {
|
|
|
|
(Some(E::V), _) => todo!(),
|
|
|
|
(_, _) => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// lint
|
|
|
|
match (Some(42), Some(E::V), Some(42)) {
|
|
|
|
(.., Some(E::V), _) => {},
|
|
|
|
(..) => {},
|
|
|
|
}
|
|
|
|
|
2022-01-20 22:24:07 -06:00
|
|
|
// Don't lint, see above.
|
2022-01-20 05:50:14 -06:00
|
|
|
match (Some(E::V), Some(E::V), Some(E::V)) {
|
|
|
|
(.., Some(E::V), _) => {},
|
|
|
|
(.., None, _) => {},
|
|
|
|
}
|
|
|
|
|
2022-01-20 22:24:07 -06:00
|
|
|
// Don't lint, see above.
|
2022-01-20 05:50:14 -06:00
|
|
|
match (Some(E::V), Some(E::V), Some(E::V)) {
|
|
|
|
(Some(E::V), ..) => {},
|
|
|
|
(None, ..) => {},
|
|
|
|
}
|
|
|
|
|
2022-01-20 22:24:07 -06:00
|
|
|
// Don't lint, see above.
|
2022-01-20 05:50:14 -06:00
|
|
|
match (Some(E::V), Some(E::V), Some(E::V)) {
|
|
|
|
(_, Some(E::V), ..) => {},
|
|
|
|
(_, None, ..) => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:20:35 -06:00
|
|
|
fn skip_type_aliases() {
|
|
|
|
enum OptionEx {
|
|
|
|
Some(i32),
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
enum ResultEx {
|
|
|
|
Err(i32),
|
|
|
|
Ok(i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
use OptionEx::{None, Some};
|
|
|
|
use ResultEx::{Err, Ok};
|
|
|
|
|
|
|
|
// don't lint
|
|
|
|
match Err(42) {
|
|
|
|
Ok(_) => dummy(),
|
|
|
|
Err(_) => (),
|
|
|
|
};
|
|
|
|
|
|
|
|
// don't lint
|
|
|
|
match Some(1i32) {
|
|
|
|
Some(_) => dummy(),
|
|
|
|
None => (),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-31 05:19:27 -05:00
|
|
|
macro_rules! single_match {
|
|
|
|
($num:literal) => {
|
|
|
|
match $num {
|
|
|
|
15 => println!("15"),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-31 05:43:51 -05:00
|
|
|
fn main() {
|
|
|
|
single_match!(5);
|
2022-02-17 12:43:59 -06:00
|
|
|
|
|
|
|
// Don't lint
|
|
|
|
let _ = match Some(0) {
|
|
|
|
#[cfg(feature = "foo")]
|
|
|
|
Some(10) => 11,
|
|
|
|
Some(x) => x,
|
|
|
|
_ => 0,
|
|
|
|
};
|
2020-03-31 05:43:51 -05:00
|
|
|
}
|
2023-05-21 22:33:42 -05:00
|
|
|
|
|
|
|
fn issue_10808(bar: Option<i32>) {
|
|
|
|
match bar {
|
|
|
|
Some(v) => unsafe {
|
|
|
|
let r = &v as *const i32;
|
|
|
|
println!("{}", *r);
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
match bar {
|
2023-05-22 19:02:45 -05:00
|
|
|
#[rustfmt::skip]
|
2023-05-21 22:33:42 -05:00
|
|
|
Some(v) => {
|
|
|
|
unsafe {
|
|
|
|
let r = &v as *const i32;
|
|
|
|
println!("{}", *r);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
2023-06-19 10:18:27 -05:00
|
|
|
|
|
|
|
mod issue8634 {
|
|
|
|
struct SomeError(i32, i32);
|
|
|
|
|
|
|
|
fn foo(x: Result<i32, ()>) {
|
|
|
|
match x {
|
|
|
|
Ok(y) => {
|
|
|
|
println!("Yay! {y}");
|
|
|
|
},
|
|
|
|
Err(()) => {
|
|
|
|
// Ignore this error because blah blah blah.
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(x: Result<i32, SomeError>) {
|
|
|
|
match x {
|
|
|
|
Ok(y) => {
|
|
|
|
println!("Yay! {y}");
|
|
|
|
},
|
|
|
|
Err(_) => {
|
|
|
|
// TODO: Process the error properly.
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-06-19 13:07:31 -05:00
|
|
|
|
|
|
|
fn block_comment(x: Result<i32, SomeError>) {
|
|
|
|
match x {
|
|
|
|
Ok(y) => {
|
|
|
|
println!("Yay! {y}");
|
|
|
|
},
|
|
|
|
Err(_) => {
|
|
|
|
/*
|
|
|
|
let's make sure that this also
|
|
|
|
does not lint block comments.
|
|
|
|
*/
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-06-19 10:18:27 -05:00
|
|
|
}
|
2023-08-31 00:24:49 -05:00
|
|
|
|
|
|
|
fn issue11365() {
|
|
|
|
enum Foo {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
}
|
|
|
|
use Foo::{A, B, C};
|
|
|
|
|
|
|
|
match Some(A) {
|
|
|
|
Some(A | B | C) => println!(),
|
|
|
|
None => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
match Some(A) {
|
|
|
|
Some(A | B) => println!(),
|
|
|
|
Some { 0: C } | None => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
match [A, A] {
|
|
|
|
[A, _] => println!(),
|
|
|
|
[_, A | B | C] => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
match Ok::<_, u32>(Some(A)) {
|
|
|
|
Ok(Some(A)) => println!(),
|
|
|
|
Err(_) | Ok(None | Some(B | C)) => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
match Ok::<_, u32>(Some(A)) {
|
|
|
|
Ok(Some(A)) => println!(),
|
|
|
|
Err(_) | Ok(None | Some(_)) => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
match &Some(A) {
|
|
|
|
Some(A | B | C) => println!(),
|
|
|
|
None => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
match &Some(A) {
|
|
|
|
&Some(A | B | C) => println!(),
|
|
|
|
None => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
match &Some(A) {
|
|
|
|
Some(A | B) => println!(),
|
|
|
|
None | Some(_) => {},
|
|
|
|
}
|
|
|
|
}
|
2024-08-30 16:02:07 -05:00
|
|
|
|
|
|
|
#[derive(Eq, PartialEq)]
|
|
|
|
pub struct Data([u8; 4]);
|
|
|
|
|
|
|
|
const DATA: Data = Data([1, 2, 3, 4]);
|
|
|
|
const CONST_I32: i32 = 1;
|
|
|
|
|
|
|
|
fn irrefutable_match() {
|
|
|
|
match DATA {
|
|
|
|
DATA => println!(),
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
match CONST_I32 {
|
|
|
|
CONST_I32 => println!(),
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
match i {
|
|
|
|
i => {
|
|
|
|
let a = 1;
|
|
|
|
let b = 2;
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
2024-09-06 10:10:42 -05:00
|
|
|
|
|
|
|
match i {
|
|
|
|
i => {},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
match i {
|
|
|
|
i => (),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match CONST_I32 {
|
|
|
|
CONST_I32 => println!(),
|
|
|
|
_ => {},
|
|
|
|
}
|
2024-08-30 16:02:07 -05:00
|
|
|
}
|