Skip to content

Commit

Permalink
Auto merge of #8080 - dswij:8019, r=giraffate
Browse files Browse the repository at this point in the history
Fix FP on `question_mark` if returned object is not local

Closes #8019

changelog: [`question_mark`] Fix FP when returned object is not local
  • Loading branch information
bors committed Dec 7, 2021
2 parents 48d939f + 01ca66c commit f615ea4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,24 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
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);
Expand All @@ -157,4 +175,5 @@ fn main() {
func();

let _ = result_func(Ok(42));
let _ = f();
}
19 changes: 19 additions & 0 deletions tests/ui/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
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);
Expand All @@ -189,4 +207,5 @@ fn main() {
func();

let _ = result_func(Ok(42));
let _ = f();
}

0 comments on commit f615ea4

Please sign in to comment.