| #![warn(clippy::redundant_pattern_matching)] | |
| #![allow( | |
| clippy::needless_bool, | |
| clippy::needless_ifs, | |
| clippy::match_like_matches_macro, | |
| clippy::equatable_if_let, | |
| clippy::if_same_then_else | |
| )] | |
| fn issue_11174<T>(boolean: bool, maybe_some: Option<T>) -> bool { | |
| maybe_some.is_none() && (!boolean) | |
| //~^ redundant_pattern_matching | |
| } | |
| fn issue_11174_edge_cases<T>(boolean: bool, boolean2: bool, maybe_some: Option<T>) { | |
| let _ = maybe_some.is_none() && (boolean || boolean2); // guard needs parentheses | |
| // | |
| //~^^ redundant_pattern_matching | |
| let _ = match maybe_some { | |
| // can't use `matches!` here | |
| // because `expr` metavars in macros don't allow let exprs | |
| None if let Some(x) = Some(0) | |
| && x > 5 => | |
| { | |
| true | |
| }, | |
| _ => false, | |
| }; | |
| } | |
| fn main() { | |
| if None::<()>.is_none() {} | |
| //~^ redundant_pattern_matching | |
| if Some(42).is_some() {} | |
| //~^ redundant_pattern_matching | |
| if Some(42).is_some() { | |
| //~^ redundant_pattern_matching | |
| foo(); | |
| } else { | |
| bar(); | |
| } | |
| while Some(42).is_some() {} | |
| //~^ redundant_pattern_matching | |
| while Some(42).is_none() {} | |
| //~^ redundant_pattern_matching | |
| while None::<()>.is_none() {} | |
| //~^ redundant_pattern_matching | |
| let mut v = vec![1, 2, 3]; | |
| while v.pop().is_some() { | |
| //~^ redundant_pattern_matching | |
| foo(); | |
| } | |
| if None::<i32>.is_none() {} | |
| if Some(42).is_some() {} | |
| Some(42).is_some(); | |
| None::<()>.is_none(); | |
| let _ = None::<()>.is_none(); | |
| let opt = Some(false); | |
| let _ = if opt.is_some() { true } else { false }; | |
| //~^ redundant_pattern_matching | |
| issue6067(); | |
| issue10726(); | |
| issue10803(); | |
| let _ = if gen_opt().is_some() { | |
| //~^ redundant_pattern_matching | |
| 1 | |
| } else if gen_opt().is_none() { | |
| //~^ redundant_pattern_matching | |
| 2 | |
| } else { | |
| 3 | |
| }; | |
| if gen_opt().is_some() {} | |
| //~^ redundant_pattern_matching | |
| } | |
| 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 Some(42).is_some() {} | |
| //~^ redundant_pattern_matching | |
| if None::<()>.is_none() {} | |
| //~^ redundant_pattern_matching | |
| while Some(42).is_some() {} | |
| //~^ redundant_pattern_matching | |
| while None::<()>.is_none() {} | |
| //~^ redundant_pattern_matching | |
| Some(42).is_some(); | |
| None::<()>.is_none(); | |
| } | |
| #[allow(clippy::deref_addrof, dead_code, clippy::needless_borrow)] | |
| fn issue7921() { | |
| if (&None::<()>).is_none() {} | |
| //~^ redundant_pattern_matching | |
| if (&None::<()>).is_none() {} | |
| //~^ redundant_pattern_matching | |
| } | |
| fn issue10726() { | |
| let x = Some(42); | |
| x.is_some(); | |
| x.is_none(); | |
| x.is_none(); | |
| x.is_some(); | |
| // Don't lint | |
| match x { | |
| Some(21) => true, | |
| _ => false, | |
| }; | |
| } | |
| fn issue10803() { | |
| let x = Some(42); | |
| let _ = x.is_some(); | |
| //~^ redundant_pattern_matching | |
| let _ = x.is_none(); | |
| //~^ redundant_pattern_matching | |
| // Don't lint | |
| let _ = matches!(x, Some(16)); | |
| } | |
| fn issue13902() { | |
| let x = Some(0); | |
| let p = &raw const x; | |
| unsafe { | |
| let _ = (*p).is_none(); | |
| //~^ redundant_pattern_matching | |
| } | |
| } | |
| fn issue16045() { | |
| fn f() -> Result<(), ()> { | |
| let x = Ok::<_, ()>(Some(123)); | |
| if x?.is_some() { | |
| //~^ redundant_pattern_matching | |
| } | |
| Ok(()) | |
| } | |
| async fn g() { | |
| struct F { | |
| x: Option<u32>, | |
| } | |
| impl Future for F { | |
| type Output = Option<u32>; | |
| fn poll(self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> { | |
| std::task::Poll::Ready(self.x) | |
| } | |
| } | |
| let x = F { x: Some(123) }; | |
| if x.await.is_some() { | |
| //~^ redundant_pattern_matching | |
| } | |
| } | |
| } | |
| fn issue14989() { | |
| macro_rules! x { | |
| () => { | |
| None::<i32> | |
| }; | |
| } | |
| if x! {}.is_some() {}; | |
| //~^ redundant_pattern_matching | |
| while x! {}.is_some() {} | |
| //~^ redundant_pattern_matching | |
| } |