2022-02-25 10:08:52 +00:00
|
|
|
// run-rustfix
|
2022-03-10 01:44:25 +00:00
|
|
|
#![warn(clippy::needless_match)]
|
2022-02-25 10:08:52 +00:00
|
|
|
#![allow(clippy::manual_map)]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2022-03-08 10:15:11 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2022-03-16 08:26:56 +00:00
|
|
|
enum Simple {
|
2022-03-07 10:12:35 +00:00
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
2022-03-08 09:37:53 +00:00
|
|
|
D,
|
2022-03-07 10:12:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 06:46:58 +00:00
|
|
|
fn useless_match() {
|
2022-03-16 08:26:56 +00:00
|
|
|
let i = 10;
|
2022-03-10 06:46:58 +00:00
|
|
|
let _: i32 = match i {
|
2022-03-07 10:12:35 +00:00
|
|
|
0 => 0,
|
|
|
|
1 => 1,
|
|
|
|
2 => 2,
|
2022-03-10 06:46:58 +00:00
|
|
|
_ => i,
|
|
|
|
};
|
|
|
|
let s = "test";
|
|
|
|
let _: &str = match s {
|
|
|
|
"a" => "a",
|
|
|
|
"b" => "b",
|
|
|
|
s => s,
|
2022-03-08 09:37:53 +00:00
|
|
|
};
|
2022-03-07 10:12:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 08:26:56 +00:00
|
|
|
fn custom_type_match() {
|
|
|
|
let se = Simple::A;
|
|
|
|
let _: Simple = match se {
|
|
|
|
Simple::A => Simple::A,
|
|
|
|
Simple::B => Simple::B,
|
|
|
|
Simple::C => Simple::C,
|
|
|
|
Simple::D => Simple::D,
|
2022-03-08 09:37:53 +00:00
|
|
|
};
|
|
|
|
// Don't trigger
|
2022-03-16 08:26:56 +00:00
|
|
|
let _: Simple = match se {
|
|
|
|
Simple::A => Simple::A,
|
|
|
|
Simple::B => Simple::B,
|
|
|
|
_ => Simple::C,
|
2022-03-08 09:37:53 +00:00
|
|
|
};
|
|
|
|
// Mingled, don't trigger
|
2022-03-16 08:26:56 +00:00
|
|
|
let _: Simple = match se {
|
|
|
|
Simple::A => Simple::B,
|
|
|
|
Simple::B => Simple::C,
|
|
|
|
Simple::C => Simple::D,
|
|
|
|
Simple::D => Simple::A,
|
2022-03-08 09:37:53 +00:00
|
|
|
};
|
2022-03-07 10:12:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 09:37:53 +00:00
|
|
|
fn option_match(x: Option<i32>) {
|
|
|
|
let _: Option<i32> = match x {
|
2022-02-25 10:08:52 +00:00
|
|
|
Some(a) => Some(a),
|
2022-03-07 10:12:35 +00:00
|
|
|
None => None,
|
2022-03-08 09:37:53 +00:00
|
|
|
};
|
|
|
|
// Don't trigger, this is the case for manual_map_option
|
|
|
|
let _: Option<i32> = match x {
|
|
|
|
Some(a) => Some(-a),
|
|
|
|
None => None,
|
|
|
|
};
|
2022-02-25 10:08:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 09:37:53 +00:00
|
|
|
fn func_ret_err<T>(err: T) -> Result<i32, T> {
|
|
|
|
Err(err)
|
2022-02-25 10:08:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 09:37:53 +00:00
|
|
|
fn result_match() {
|
|
|
|
let _: Result<i32, i32> = match Ok(1) {
|
2022-03-07 10:12:35 +00:00
|
|
|
Ok(a) => Ok(a),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
};
|
2022-03-08 09:37:53 +00:00
|
|
|
let _: Result<i32, i32> = match func_ret_err(0_i32) {
|
|
|
|
Err(err) => Err(err),
|
|
|
|
Ok(a) => Ok(a),
|
|
|
|
};
|
2022-03-16 08:26:56 +00:00
|
|
|
// as ref, don't trigger
|
|
|
|
let res = &func_ret_err(0_i32);
|
|
|
|
let _: Result<&i32, &i32> = match *res {
|
|
|
|
Ok(ref x) => Ok(x),
|
|
|
|
Err(ref x) => Err(x),
|
|
|
|
};
|
2022-03-07 10:12:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 15:06:31 +00:00
|
|
|
fn if_let_option() {
|
|
|
|
let _ = if let Some(a) = Some(1) { Some(a) } else { None };
|
|
|
|
|
|
|
|
fn do_something() {}
|
|
|
|
|
|
|
|
// Don't trigger
|
|
|
|
let _ = if let Some(a) = Some(1) {
|
|
|
|
Some(a)
|
|
|
|
} else {
|
|
|
|
do_something();
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// Don't trigger
|
|
|
|
let _ = if let Some(a) = Some(1) {
|
|
|
|
do_something();
|
|
|
|
Some(a)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2022-02-25 10:08:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 08:26:56 +00:00
|
|
|
fn if_let_result() {
|
|
|
|
let x: Result<i32, i32> = Ok(1);
|
|
|
|
let _: Result<i32, i32> = if let Err(e) = x { Err(e) } else { x };
|
|
|
|
let _: Result<i32, i32> = if let Ok(val) = x { Ok(val) } else { x };
|
2022-03-08 09:37:53 +00:00
|
|
|
// Input type mismatch, don't trigger
|
2022-03-16 08:26:56 +00:00
|
|
|
let _: Result<i32, i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
|
2022-02-25 10:08:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 08:26:56 +00:00
|
|
|
fn if_let_custom_enum(x: Simple) {
|
|
|
|
let _: Simple = if let Simple::A = x {
|
|
|
|
Simple::A
|
|
|
|
} else if let Simple::B = x {
|
|
|
|
Simple::B
|
|
|
|
} else if let Simple::C = x {
|
|
|
|
Simple::C
|
2022-02-25 10:08:52 +00:00
|
|
|
} else {
|
2022-03-08 09:37:53 +00:00
|
|
|
x
|
2022-03-08 10:15:11 +00:00
|
|
|
};
|
2022-03-16 08:26:56 +00:00
|
|
|
|
2022-03-08 10:15:11 +00:00
|
|
|
// Don't trigger
|
2022-03-16 08:26:56 +00:00
|
|
|
let _: Simple = if let Simple::A = x {
|
|
|
|
Simple::A
|
2022-03-08 10:15:11 +00:00
|
|
|
} else if true {
|
2022-03-16 08:26:56 +00:00
|
|
|
Simple::B
|
2022-03-08 10:15:11 +00:00
|
|
|
} else {
|
|
|
|
x
|
|
|
|
};
|
2022-02-25 10:08:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 08:26:56 +00:00
|
|
|
mod issue8542 {
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum E {
|
|
|
|
VariantA(u8, u8),
|
|
|
|
VariantB(u8, bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Complex {
|
|
|
|
A(u8),
|
|
|
|
B(u8, bool),
|
|
|
|
C(u8, i32, f64),
|
|
|
|
D(E, bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_test() {
|
|
|
|
let ce = Complex::B(8, false);
|
|
|
|
let aa = 0_u8;
|
|
|
|
let bb = false;
|
|
|
|
|
|
|
|
let _: Complex = match ce {
|
|
|
|
Complex::A(a) => Complex::A(a),
|
|
|
|
Complex::B(a, b) => Complex::B(a, b),
|
|
|
|
Complex::C(a, b, c) => Complex::C(a, b, c),
|
|
|
|
Complex::D(E::VariantA(ea, eb), b) => Complex::D(E::VariantA(ea, eb), b),
|
|
|
|
Complex::D(E::VariantB(ea, eb), b) => Complex::D(E::VariantB(ea, eb), b),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Don't trigger
|
|
|
|
let _: Complex = match ce {
|
|
|
|
Complex::A(_) => Complex::A(aa),
|
|
|
|
Complex::B(_, b) => Complex::B(aa, b),
|
|
|
|
Complex::C(_, b, _) => Complex::C(aa, b, 64_f64),
|
|
|
|
Complex::D(e, b) => Complex::D(e, b),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Don't trigger
|
|
|
|
let _: Complex = match ce {
|
|
|
|
Complex::A(a) => Complex::A(a),
|
|
|
|
Complex::B(a, _) => Complex::B(a, bb),
|
|
|
|
Complex::C(a, _, _) => Complex::C(a, 32_i32, 64_f64),
|
|
|
|
_ => ce,
|
|
|
|
};
|
|
|
|
}
|
2022-03-17 15:06:31 +00:00
|
|
|
}
|
2022-03-16 08:26:56 +00:00
|
|
|
|
2022-03-17 15:06:31 +00:00
|
|
|
/// Lint triggered when type coercions happen.
|
|
|
|
/// Do NOT trigger on any of these.
|
|
|
|
mod issue8551 {
|
|
|
|
trait Trait {}
|
|
|
|
struct Struct;
|
|
|
|
impl Trait for Struct {}
|
|
|
|
|
|
|
|
fn optmap(s: Option<&Struct>) -> Option<&dyn Trait> {
|
|
|
|
match s {
|
|
|
|
Some(s) => Some(s),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
2022-03-16 08:26:56 +00:00
|
|
|
|
2022-03-17 15:06:31 +00:00
|
|
|
fn lint_tests() {
|
|
|
|
let option: Option<&Struct> = None;
|
|
|
|
let _: Option<&dyn Trait> = match option {
|
|
|
|
Some(s) => Some(s),
|
|
|
|
None => None,
|
2022-03-16 08:26:56 +00:00
|
|
|
};
|
|
|
|
|
2022-03-17 15:06:31 +00:00
|
|
|
let _: Option<&dyn Trait> = if true {
|
|
|
|
match option {
|
|
|
|
Some(s) => Some(s),
|
|
|
|
None => None,
|
|
|
|
}
|
2022-03-16 08:26:56 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2022-03-17 15:06:31 +00:00
|
|
|
|
|
|
|
let result: Result<&Struct, i32> = Err(0);
|
|
|
|
let _: Result<&dyn Trait, i32> = match result {
|
|
|
|
Ok(s) => Ok(s),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
};
|
|
|
|
|
|
|
|
let _: Option<&dyn Trait> = if let Some(s) = option { Some(s) } else { None };
|
2022-03-16 08:26:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 07:23:19 +00:00
|
|
|
trait Tr {
|
|
|
|
fn as_mut(&mut self) -> Result<&mut i32, &mut i32>;
|
|
|
|
}
|
|
|
|
impl Tr for Result<i32, i32> {
|
|
|
|
fn as_mut(&mut self) -> Result<&mut i32, &mut i32> {
|
|
|
|
match self {
|
|
|
|
Ok(x) => Ok(x),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:12:35 +00:00
|
|
|
fn main() {}
|