From 01ca66cbd70ecfa7ba6294219ecba9f6ad9c8b2b Mon Sep 17 00:00:00 2001 From: dswij Date: Mon, 6 Dec 2021 17:18:17 +0800 Subject: [PATCH] Fix FP on `question_mark` if returned object is not local --- clippy_lints/src/question_mark.rs | 2 +- tests/ui/question_mark.fixed | 19 +++++++++++++++++++ tests/ui/question_mark.rs | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/question_mark.rs b/clippy_lints/src/question_mark.rs index a5531993ee6..7d30c34e956 100644 --- a/clippy_lints/src/question_mark.rs +++ b/clippy_lints/src/question_mark.rs @@ -183,7 +183,7 @@ fn expression_returns_unmodified_err(cx: &LateContext<'_>, expr: &Expr<'_>, cond false }, ExprKind::Ret(Some(ret_expr)) => Self::expression_returns_unmodified_err(cx, ret_expr, cond_expr), - ExprKind::Path(_) => path_to_local(expr) == path_to_local(cond_expr), + ExprKind::Path(_) => path_to_local(expr).is_some() && path_to_local(expr) == path_to_local(cond_expr), _ => false, } } diff --git a/tests/ui/question_mark.fixed b/tests/ui/question_mark.fixed index e93469e5f55..13ce0f32d4b 100644 --- a/tests/ui/question_mark.fixed +++ b/tests/ui/question_mark.fixed @@ -136,6 +136,24 @@ fn result_func(x: Result) -> Result { Ok(y) } +// see issue #8019 +pub enum NotOption { + None, + First, + AfterFirst, +} + +fn obj(_: i32) -> Result<(), NotOption> { + Err(NotOption::First) +} + +fn f() -> NotOption { + if obj(2).is_err() { + return NotOption::None; + } + NotOption::First +} + fn main() { some_func(Some(42)); some_func(None); @@ -157,4 +175,5 @@ fn main() { func(); let _ = result_func(Ok(42)); + let _ = f(); } diff --git a/tests/ui/question_mark.rs b/tests/ui/question_mark.rs index dd179e9bee8..60590fd9311 100644 --- a/tests/ui/question_mark.rs +++ b/tests/ui/question_mark.rs @@ -168,6 +168,24 @@ fn result_func(x: Result) -> Result { Ok(y) } +// see issue #8019 +pub enum NotOption { + None, + First, + AfterFirst, +} + +fn obj(_: i32) -> Result<(), NotOption> { + Err(NotOption::First) +} + +fn f() -> NotOption { + if obj(2).is_err() { + return NotOption::None; + } + NotOption::First +} + fn main() { some_func(Some(42)); some_func(None); @@ -189,4 +207,5 @@ fn main() { func(); let _ = result_func(Ok(42)); + let _ = f(); }