diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/needless_match.fixed | 1 | ||||
| -rw-r--r-- | tests/ui/needless_match.rs | 1 | ||||
| -rw-r--r-- | tests/ui/needless_match.stderr | 4 | ||||
| -rw-r--r-- | tests/ui/question_mark.fixed | 65 | ||||
| -rw-r--r-- | tests/ui/question_mark.rs | 69 | ||||
| -rw-r--r-- | tests/ui/question_mark.stderr | 52 |
6 files changed, 138 insertions, 54 deletions
diff --git a/tests/ui/needless_match.fixed b/tests/ui/needless_match.fixed index b997e5316cf..0c9178fb85e 100644 --- a/tests/ui/needless_match.fixed +++ b/tests/ui/needless_match.fixed @@ -99,6 +99,7 @@ fn if_let_result() { let _: Result<i32, i32> = x; let _: Result<i32, i32> = x; // Input type mismatch, don't trigger + #[allow(clippy::question_mark)] let _: Result<i32, i32> = if let Err(e) = Ok(1) { Err(e) } else { x }; } diff --git a/tests/ui/needless_match.rs b/tests/ui/needless_match.rs index 90482775a1e..f66f01d7cca 100644 --- a/tests/ui/needless_match.rs +++ b/tests/ui/needless_match.rs @@ -122,6 +122,7 @@ fn if_let_result() { let _: Result<i32, i32> = if let Err(e) = x { Err(e) } else { x }; let _: Result<i32, i32> = if let Ok(val) = x { Ok(val) } else { x }; // Input type mismatch, don't trigger + #[allow(clippy::question_mark)] let _: Result<i32, i32> = if let Err(e) = Ok(1) { Err(e) } else { x }; } diff --git a/tests/ui/needless_match.stderr b/tests/ui/needless_match.stderr index 2d679631c6f..5bc79800a1a 100644 --- a/tests/ui/needless_match.stderr +++ b/tests/ui/needless_match.stderr @@ -84,7 +84,7 @@ LL | let _: Result<i32, i32> = if let Ok(val) = x { Ok(val) } else { x }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x` error: this if-let expression is unnecessary - --> $DIR/needless_match.rs:129:21 + --> $DIR/needless_match.rs:130:21 | LL | let _: Simple = if let Simple::A = x { | _____________________^ @@ -97,7 +97,7 @@ LL | | }; | |_____^ help: replace it with: `x` error: this match expression is unnecessary - --> $DIR/needless_match.rs:168:26 + --> $DIR/needless_match.rs:169:26 | LL | let _: Complex = match ce { | __________________________^ diff --git a/tests/ui/question_mark.fixed b/tests/ui/question_mark.fixed index 13ce0f32d4b..c4c9c821433 100644 --- a/tests/ui/question_mark.fixed +++ b/tests/ui/question_mark.fixed @@ -1,5 +1,6 @@ // run-rustfix #![allow(unreachable_code)] +#![allow(dead_code)] #![allow(clippy::unnecessary_wraps)] fn some_func(a: Option<u32>) -> Option<u32> { @@ -154,26 +155,56 @@ fn f() -> NotOption { NotOption::First } -fn main() { - some_func(Some(42)); - some_func(None); - some_other_func(Some(42)); +fn do_something() {} - let copy_struct = CopyStruct { opt: Some(54) }; - copy_struct.func(); +fn err_immediate_return() -> Result<i32, i32> { + func_returning_result()?; + Ok(1) +} - let move_struct = MoveStruct { - opt: Some(vec![42, 1337]), - }; - move_struct.ref_func(); - move_struct.clone().mov_func_reuse(); - move_struct.mov_func_no_use(); +fn err_immediate_return_and_do_something() -> Result<i32, i32> { + func_returning_result()?; + do_something(); + Ok(1) +} - let so = SeemsOption::Some(45); - returns_something_similar_to_option(so); +// No warning +fn no_immediate_return() -> Result<i32, i32> { + if let Err(err) = func_returning_result() { + do_something(); + return Err(err); + } + Ok(1) +} - func(); +// No warning +fn mixed_result_and_option() -> Option<i32> { + if let Err(err) = func_returning_result() { + return Some(err); + } + None +} + +// No warning +fn else_if_check() -> Result<i32, i32> { + if true { + Ok(1) + } else if let Err(e) = func_returning_result() { + Err(e) + } else { + Err(-1) + } +} - let _ = result_func(Ok(42)); - let _ = f(); +// No warning +#[allow(clippy::manual_map)] +#[rustfmt::skip] +fn option_map() -> Option<bool> { + if let Some(a) = Some(false) { + Some(!a) + } else { + None + } } + +fn main() {} diff --git a/tests/ui/question_mark.rs b/tests/ui/question_mark.rs index 60590fd9311..cdbc7b1606f 100644 --- a/tests/ui/question_mark.rs +++ b/tests/ui/question_mark.rs @@ -1,5 +1,6 @@ // run-rustfix #![allow(unreachable_code)] +#![allow(dead_code)] #![allow(clippy::unnecessary_wraps)] fn some_func(a: Option<u32>) -> Option<u32> { @@ -186,26 +187,60 @@ fn f() -> NotOption { NotOption::First } -fn main() { - some_func(Some(42)); - some_func(None); - some_other_func(Some(42)); +fn do_something() {} - let copy_struct = CopyStruct { opt: Some(54) }; - copy_struct.func(); +fn err_immediate_return() -> Result<i32, i32> { + if let Err(err) = func_returning_result() { + return Err(err); + } + Ok(1) +} - let move_struct = MoveStruct { - opt: Some(vec![42, 1337]), - }; - move_struct.ref_func(); - move_struct.clone().mov_func_reuse(); - move_struct.mov_func_no_use(); +fn err_immediate_return_and_do_something() -> Result<i32, i32> { + if let Err(err) = func_returning_result() { + return Err(err); + } + do_something(); + Ok(1) +} - let so = SeemsOption::Some(45); - returns_something_similar_to_option(so); +// No warning +fn no_immediate_return() -> Result<i32, i32> { + if let Err(err) = func_returning_result() { + do_something(); + return Err(err); + } + Ok(1) +} - func(); +// No warning +fn mixed_result_and_option() -> Option<i32> { + if let Err(err) = func_returning_result() { + return Some(err); + } + None +} - let _ = result_func(Ok(42)); - let _ = f(); +// No warning +fn else_if_check() -> Result<i32, i32> { + if true { + Ok(1) + } else if let Err(e) = func_returning_result() { + Err(e) + } else { + Err(-1) + } } + +// No warning +#[allow(clippy::manual_map)] +#[rustfmt::skip] +fn option_map() -> Option<bool> { + if let Some(a) = Some(false) { + Some(!a) + } else { + None + } +} + +fn main() {} diff --git a/tests/ui/question_mark.stderr b/tests/ui/question_mark.stderr index 8d782b71dd6..1b6cd524b2f 100644 --- a/tests/ui/question_mark.stderr +++ b/tests/ui/question_mark.stderr @@ -1,5 +1,5 @@ error: this block may be rewritten with the `?` operator - --> $DIR/question_mark.rs:6:5 + --> $DIR/question_mark.rs:7:5 | LL | / if a.is_none() { LL | | return None; @@ -9,7 +9,7 @@ LL | | } = note: `-D clippy::question-mark` implied by `-D warnings` error: this block may be rewritten with the `?` operator - --> $DIR/question_mark.rs:51:9 + --> $DIR/question_mark.rs:52:9 | LL | / if (self.opt).is_none() { LL | | return None; @@ -17,7 +17,7 @@ LL | | } | |_________^ help: replace it with: `(self.opt)?;` error: this block may be rewritten with the `?` operator - --> $DIR/question_mark.rs:55:9 + --> $DIR/question_mark.rs:56:9 | LL | / if self.opt.is_none() { LL | | return None @@ -25,7 +25,7 @@ LL | | } | |_________^ help: replace it with: `self.opt?;` error: this block may be rewritten with the `?` operator - --> $DIR/question_mark.rs:59:17 + --> $DIR/question_mark.rs:60:17 | LL | let _ = if self.opt.is_none() { | _________________^ @@ -35,8 +35,8 @@ LL | | self.opt LL | | }; | |_________^ help: replace it with: `Some(self.opt?)` -error: this if-let-else may be rewritten with the `?` operator - --> $DIR/question_mark.rs:65:17 +error: this block may be rewritten with the `?` operator + --> $DIR/question_mark.rs:66:17 | LL | let _ = if let Some(x) = self.opt { | _________________^ @@ -47,7 +47,7 @@ LL | | }; | |_________^ help: replace it with: `self.opt?` error: this block may be rewritten with the `?` operator - --> $DIR/question_mark.rs:82:9 + --> $DIR/question_mark.rs:83:9 | LL | / if self.opt.is_none() { LL | | return None; @@ -55,7 +55,7 @@ LL | | } | |_________^ help: replace it with: `self.opt.as_ref()?;` error: this block may be rewritten with the `?` operator - --> $DIR/question_mark.rs:90:9 + --> $DIR/question_mark.rs:91:9 | LL | / if self.opt.is_none() { LL | | return None; @@ -63,15 +63,15 @@ LL | | } | |_________^ help: replace it with: `self.opt.as_ref()?;` error: this block may be rewritten with the `?` operator - --> $DIR/question_mark.rs:98:9 + --> $DIR/question_mark.rs:99:9 | LL | / if self.opt.is_none() { LL | | return None; LL | | } | |_________^ help: replace it with: `self.opt.as_ref()?;` -error: this if-let-else may be rewritten with the `?` operator - --> $DIR/question_mark.rs:105:26 +error: this block may be rewritten with the `?` operator + --> $DIR/question_mark.rs:106:26 | LL | let v: &Vec<_> = if let Some(ref v) = self.opt { | __________________________^ @@ -81,8 +81,8 @@ LL | | return None; LL | | }; | |_________^ help: replace it with: `self.opt.as_ref()?` -error: this if-let-else may be rewritten with the `?` operator - --> $DIR/question_mark.rs:115:17 +error: this block may be rewritten with the `?` operator + --> $DIR/question_mark.rs:116:17 | LL | let v = if let Some(v) = self.opt { | _________________^ @@ -93,26 +93,42 @@ LL | | }; | |_________^ help: replace it with: `self.opt?` error: this block may be rewritten with the `?` operator - --> $DIR/question_mark.rs:130:5 + --> $DIR/question_mark.rs:131:5 | LL | / if f().is_none() { LL | | return None; LL | | } | |_____^ help: replace it with: `f()?;` -error: this if-let-else may be rewritten with the `?` operator - --> $DIR/question_mark.rs:142:13 +error: this block may be rewritten with the `?` operator + --> $DIR/question_mark.rs:143:13 | LL | let _ = if let Ok(x) = x { x } else { return x }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x?` error: this block may be rewritten with the `?` operator - --> $DIR/question_mark.rs:144:5 + --> $DIR/question_mark.rs:145:5 | LL | / if x.is_err() { LL | | return x; LL | | } | |_____^ help: replace it with: `x?;` -error: aborting due to 13 previous errors +error: this block may be rewritten with the `?` operator + --> $DIR/question_mark.rs:193:5 + | +LL | / if let Err(err) = func_returning_result() { +LL | | return Err(err); +LL | | } + | |_____^ help: replace it with: `func_returning_result()?;` + +error: this block may be rewritten with the `?` operator + --> $DIR/question_mark.rs:200:5 + | +LL | / if let Err(err) = func_returning_result() { +LL | | return Err(err); +LL | | } + | |_____^ help: replace it with: `func_returning_result()?;` + +error: aborting due to 15 previous errors |
