Fix FP on question_mark if returned object is not local

This commit is contained in:
dswij 2021-12-06 17:18:17 +08:00
parent a5d597637d
commit 01ca66cbd7
3 changed files with 39 additions and 1 deletions

View File

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

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();
}