Auto merge of #8080 - dswij:8019, r=giraffate

Fix FP on `question_mark` if returned object is not local

Closes #8019

changelog: [`question_mark`] Fix FP when returned object is not local
This commit is contained in:
bors 2021-12-07 00:07:46 +00:00
commit f615ea474b
3 changed files with 39 additions and 1 deletions

View file

@ -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,
}
}

View file

@ -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);
@ -157,4 +175,5 @@ fn main() {
func();
let _ = result_func(Ok(42));
let _ = f();
}

View file

@ -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);
@ -189,4 +207,5 @@ fn main() {
func();
let _ = result_func(Ok(42));
let _ = f();
}