rust/tests/ui/redundant_pattern_matching_option.rs

150 lines
2.5 KiB
Rust
Raw Normal View History

//@run-rustfix
#![warn(clippy::all)]
#![warn(clippy::redundant_pattern_matching)]
#![allow(
unused_must_use,
clippy::needless_bool,
2023-06-10 06:43:30 -05:00
clippy::needless_if,
clippy::match_like_matches_macro,
2021-10-04 01:33:40 -05:00
clippy::equatable_if_let,
clippy::if_same_then_else
)]
fn main() {
if let None = None::<()> {}
if let Some(_) = Some(42) {}
if let Some(_) = Some(42) {
foo();
} else {
bar();
}
while let Some(_) = Some(42) {}
while let None = Some(42) {}
while let None = None::<()> {}
let mut v = vec![1, 2, 3];
while let Some(_) = v.pop() {
foo();
}
if None::<i32>.is_none() {}
if Some(42).is_some() {}
match Some(42) {
Some(_) => true,
None => false,
};
match None::<()> {
Some(_) => false,
None => true,
};
let _ = match None::<()> {
Some(_) => false,
None => true,
};
let opt = Some(false);
let _ = if let Some(_) = opt { true } else { false };
issue6067();
2023-05-05 14:33:16 -05:00
issue10726();
2023-05-25 23:16:39 -05:00
issue10803();
let _ = if let Some(_) = gen_opt() {
1
} else if let None = gen_opt() {
2
} else {
3
};
if let Some(..) = gen_opt() {}
}
fn gen_opt() -> Option<()> {
None
}
fn foo() {}
fn bar() {}
// Methods that are unstable const should not be suggested within a const context, see issue #5697.
// However, in Rust 1.48.0 the methods `is_some` and `is_none` of `Option` were stabilized as const,
// so the following should be linted.
const fn issue6067() {
if let Some(_) = Some(42) {}
if let None = None::<()> {}
while let Some(_) = Some(42) {}
while let None = None::<()> {}
match Some(42) {
Some(_) => true,
None => false,
};
match None::<()> {
Some(_) => false,
None => true,
};
}
#[allow(clippy::deref_addrof, dead_code, clippy::needless_borrow)]
fn issue7921() {
if let None = *(&None::<()>) {}
if let None = *&None::<()> {}
}
2023-05-05 14:33:16 -05:00
fn issue10726() {
2023-05-11 00:20:02 -05:00
let x = Some(42);
match x {
2023-05-05 14:33:16 -05:00
Some(_) => true,
_ => false,
};
2023-05-11 00:20:02 -05:00
match x {
2023-05-05 14:33:16 -05:00
None => true,
_ => false,
};
2023-05-15 12:43:22 -05:00
match x {
Some(_) => false,
_ => true,
2023-05-05 14:33:16 -05:00
};
2023-05-15 12:43:22 -05:00
match x {
None => false,
_ => true,
2023-05-05 14:33:16 -05:00
};
2023-05-11 00:20:02 -05:00
// Don't lint
match x {
2023-05-09 13:50:47 -05:00
Some(21) => true,
_ => false,
2023-05-05 14:33:16 -05:00
};
}
2023-05-25 23:16:39 -05:00
fn issue10803() {
let x = Some(42);
let _ = matches!(x, Some(_));
let _ = matches!(x, None);
// Don't lint
let _ = matches!(x, Some(16));
}