From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- src/tools/clippy/tests/ui/needless_match.fixed | 210 +++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 src/tools/clippy/tests/ui/needless_match.fixed (limited to 'src/tools/clippy/tests/ui/needless_match.fixed') diff --git a/src/tools/clippy/tests/ui/needless_match.fixed b/src/tools/clippy/tests/ui/needless_match.fixed new file mode 100644 index 000000000..0c9178fb8 --- /dev/null +++ b/src/tools/clippy/tests/ui/needless_match.fixed @@ -0,0 +1,210 @@ +// run-rustfix +#![warn(clippy::needless_match)] +#![allow(clippy::manual_map)] +#![allow(dead_code)] + +#[derive(Clone, Copy)] +enum Simple { + A, + B, + C, + D, +} + +fn useless_match() { + let i = 10; + let _: i32 = i; + let s = "test"; + let _: &str = s; +} + +fn custom_type_match() { + let se = Simple::A; + let _: Simple = se; + // Don't trigger + let _: Simple = match se { + Simple::A => Simple::A, + Simple::B => Simple::B, + _ => Simple::C, + }; + // Mingled, don't trigger + let _: Simple = match se { + Simple::A => Simple::B, + Simple::B => Simple::C, + Simple::C => Simple::D, + Simple::D => Simple::A, + }; +} + +fn option_match(x: Option) { + let _: Option = x; + // Don't trigger, this is the case for manual_map_option + let _: Option = match x { + Some(a) => Some(-a), + None => None, + }; +} + +fn func_ret_err(err: T) -> Result { + Err(err) +} + +fn result_match() { + let _: Result = Ok(1); + let _: Result = func_ret_err(0_i32); + // 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), + }; +} + +fn if_let_option() { + let _ = Some(1); + + 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 + }; + + // Don't trigger + let _ = if let Some(a) = Some(1) { Some(a) } else { Some(2) }; +} + +fn if_let_option_result() -> Result<(), ()> { + fn f(x: i32) -> Result, ()> { + Ok(Some(x)) + } + // Don't trigger + let _ = if let Some(v) = f(1)? { Some(v) } else { f(2)? }; + Ok(()) +} + +fn if_let_result() { + let x: Result = Ok(1); + let _: Result = x; + let _: Result = x; + // Input type mismatch, don't trigger + #[allow(clippy::question_mark)] + let _: Result = if let Err(e) = Ok(1) { Err(e) } else { x }; +} + +fn if_let_custom_enum(x: Simple) { + let _: Simple = x; + + // Don't trigger + let _: Simple = if let Simple::A = x { + Simple::A + } else if true { + Simple::B + } else { + x + }; +} + +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 = ce; + + // 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, + }; + } +} + +/// 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, + } + } + + fn lint_tests() { + let option: Option<&Struct> = None; + let _: Option<&dyn Trait> = match option { + Some(s) => Some(s), + None => None, + }; + + let _: Option<&dyn Trait> = if true { + match option { + Some(s) => Some(s), + None => None, + } + } else { + None + }; + + 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 }; + } +} + +trait Tr { + fn as_mut(&mut self) -> Result<&mut i32, &mut i32>; +} +impl Tr for Result { + fn as_mut(&mut self) -> Result<&mut i32, &mut i32> { + match self { + Ok(x) => Ok(x), + Err(e) => Err(e), + } + } +} + +fn main() {} -- cgit v1.2.3