2022-02-25 04:08:52 -06:00
|
|
|
// run-rustfix
|
2022-03-09 19:44:25 -06:00
|
|
|
#![warn(clippy::needless_match)]
|
2022-02-25 04:08:52 -06:00
|
|
|
#![allow(clippy::manual_map)]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2022-03-08 04:15:11 -06:00
|
|
|
#[derive(Clone, Copy)]
|
2022-03-08 03:37:53 -06:00
|
|
|
enum Choice {
|
2022-03-07 04:12:35 -06:00
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
2022-03-08 03:37:53 -06:00
|
|
|
D,
|
2022-03-07 04:12:35 -06:00
|
|
|
}
|
|
|
|
|
2022-03-10 00:46:58 -06:00
|
|
|
#[allow(unused_mut)]
|
|
|
|
fn useless_match() {
|
|
|
|
let mut i = 10;
|
|
|
|
let _: i32 = match i {
|
2022-03-07 04:12:35 -06:00
|
|
|
0 => 0,
|
|
|
|
1 => 1,
|
|
|
|
2 => 2,
|
2022-03-10 00:46:58 -06:00
|
|
|
_ => i,
|
|
|
|
};
|
|
|
|
let _: i32 = match i {
|
|
|
|
0 => 0,
|
|
|
|
1 => 1,
|
|
|
|
ref i => *i,
|
|
|
|
};
|
|
|
|
let mut _i_mut = match i {
|
|
|
|
0 => 0,
|
|
|
|
1 => 1,
|
|
|
|
ref mut i => *i,
|
|
|
|
};
|
|
|
|
|
|
|
|
let s = "test";
|
|
|
|
let _: &str = match s {
|
|
|
|
"a" => "a",
|
|
|
|
"b" => "b",
|
|
|
|
s => s,
|
2022-03-08 03:37:53 -06:00
|
|
|
};
|
2022-03-07 04:12:35 -06:00
|
|
|
}
|
|
|
|
|
2022-03-08 03:37:53 -06:00
|
|
|
fn custom_type_match(se: Choice) {
|
|
|
|
let _: Choice = match se {
|
|
|
|
Choice::A => Choice::A,
|
|
|
|
Choice::B => Choice::B,
|
|
|
|
Choice::C => Choice::C,
|
|
|
|
Choice::D => Choice::D,
|
|
|
|
};
|
|
|
|
// Don't trigger
|
|
|
|
let _: Choice = match se {
|
|
|
|
Choice::A => Choice::A,
|
|
|
|
Choice::B => Choice::B,
|
|
|
|
_ => Choice::C,
|
|
|
|
};
|
|
|
|
// Mingled, don't trigger
|
|
|
|
let _: Choice = match se {
|
|
|
|
Choice::A => Choice::B,
|
|
|
|
Choice::B => Choice::C,
|
|
|
|
Choice::C => Choice::D,
|
|
|
|
Choice::D => Choice::A,
|
|
|
|
};
|
2022-03-07 04:12:35 -06:00
|
|
|
}
|
|
|
|
|
2022-03-08 03:37:53 -06:00
|
|
|
fn option_match(x: Option<i32>) {
|
|
|
|
let _: Option<i32> = match x {
|
2022-02-25 04:08:52 -06:00
|
|
|
Some(a) => Some(a),
|
2022-03-07 04:12:35 -06:00
|
|
|
None => None,
|
2022-03-08 03:37:53 -06: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 04:08:52 -06:00
|
|
|
}
|
|
|
|
|
2022-03-08 03:37:53 -06:00
|
|
|
fn func_ret_err<T>(err: T) -> Result<i32, T> {
|
|
|
|
Err(err)
|
2022-02-25 04:08:52 -06:00
|
|
|
}
|
|
|
|
|
2022-03-08 03:37:53 -06:00
|
|
|
fn result_match() {
|
|
|
|
let _: Result<i32, i32> = match Ok(1) {
|
2022-03-07 04:12:35 -06:00
|
|
|
Ok(a) => Ok(a),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
};
|
2022-03-08 03:37:53 -06:00
|
|
|
let _: Result<i32, i32> = match func_ret_err(0_i32) {
|
|
|
|
Err(err) => Err(err),
|
|
|
|
Ok(a) => Ok(a),
|
|
|
|
};
|
2022-03-07 04:12:35 -06:00
|
|
|
}
|
|
|
|
|
2022-03-08 03:37:53 -06:00
|
|
|
fn if_let_option() -> Option<i32> {
|
2022-03-07 04:12:35 -06:00
|
|
|
if let Some(a) = Some(1) { Some(a) } else { None }
|
2022-02-25 04:08:52 -06:00
|
|
|
}
|
|
|
|
|
2022-03-08 03:37:53 -06:00
|
|
|
fn if_let_result(x: Result<(), i32>) {
|
|
|
|
let _: Result<(), i32> = if let Err(e) = x { Err(e) } else { x };
|
|
|
|
let _: Result<(), i32> = if let Ok(val) = x { Ok(val) } else { x };
|
|
|
|
// Input type mismatch, don't trigger
|
|
|
|
let _: Result<(), i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
|
2022-02-25 04:08:52 -06:00
|
|
|
}
|
|
|
|
|
2022-03-08 04:15:11 -06:00
|
|
|
fn if_let_custom_enum(x: Choice) {
|
|
|
|
let _: Choice = if let Choice::A = x {
|
2022-03-08 03:37:53 -06:00
|
|
|
Choice::A
|
|
|
|
} else if let Choice::B = x {
|
|
|
|
Choice::B
|
|
|
|
} else if let Choice::C = x {
|
|
|
|
Choice::C
|
2022-02-25 04:08:52 -06:00
|
|
|
} else {
|
2022-03-08 03:37:53 -06:00
|
|
|
x
|
2022-03-08 04:15:11 -06:00
|
|
|
};
|
|
|
|
// Don't trigger
|
|
|
|
let _: Choice = if let Choice::A = x {
|
|
|
|
Choice::A
|
|
|
|
} else if true {
|
|
|
|
Choice::B
|
|
|
|
} else {
|
|
|
|
x
|
|
|
|
};
|
2022-02-25 04:08:52 -06:00
|
|
|
}
|
|
|
|
|
2022-03-07 04:12:35 -06:00
|
|
|
fn main() {}
|