diff --git a/clippy_lints/src/question_mark.rs b/clippy_lints/src/question_mark.rs index 65a962dcd21..c765c8962cf 100644 --- a/clippy_lints/src/question_mark.rs +++ b/clippy_lints/src/question_mark.rs @@ -159,7 +159,7 @@ impl QuestionMark { fn expression_returns_unmodified_err(cx: &LateContext<'_>, expr: &Expr<'_>, cond_expr: &Expr<'_>) -> bool { match peel_blocks_with_stmt(expr).kind { 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(); }