From 949b12589112cecad9566305444527ec0738d521 Mon Sep 17 00:00:00 2001 From: nahuakang Date: Wed, 27 Jan 2021 09:34:36 +0100 Subject: [PATCH] Add unit tests for new lint --- tests/ui/for_loops_over_options.rs | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/ui/for_loops_over_options.rs diff --git a/tests/ui/for_loops_over_options.rs b/tests/ui/for_loops_over_options.rs new file mode 100644 index 00000000000..d8144864a21 --- /dev/null +++ b/tests/ui/for_loops_over_options.rs @@ -0,0 +1,31 @@ +#![warn(clippy::for_loops_over_options)] + +fn main() { + let x = vec![Some(1), Some(2), Some(3)]; + for n in x { + if let Some(n) = n { + println!("{}", n); + } + } + + let y: Vec> = vec![]; + for n in y.clone() { + if let Ok(n) = n { + println!("{}", n); + } + } + + // This should not trigger the lint + for n in y.clone() { + if let Ok(n) = n { + println!("{}", n); + } else { + println!("Oops!"); + } + } + + // This should not trigger the lint + for n in vec![Some(1), Some(2), Some(3)].iter().flatten() { + println!("{}", n); + } +}