fix: if_then_some_else_none FP when return exists in block expr

This commit is contained in:
yanglsh 2025-09-30 02:28:54 +08:00
parent 3c93ba0c1d
commit 642feb7e56
3 changed files with 25 additions and 0 deletions

View file

@ -79,6 +79,7 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
&& !is_in_const_context(cx)
&& self.msrv.meets(cx, msrvs::BOOL_THEN)
&& !contains_return(then_block.stmts)
&& then_block.expr.is_none_or(|expr| !contains_return(expr))
{
let method_name = if switch_to_eager_eval(cx, expr) && self.msrv.meets(cx, msrvs::BOOL_THEN_SOME) {
sym::then_some

View file

@ -206,3 +206,15 @@ fn dont_lint_inside_macros() {
}
let _: Option<u32> = mac!(true, 42);
}
mod issue15770 {
fn maybe_error() -> Result<u32, &'static str> {
Err("error!")
}
pub fn trying(b: bool) -> Result<(), &'static str> {
let _x: Option<u32> = if b { Some(maybe_error()?) } else { None };
// Process _x locally
Ok(())
}
}

View file

@ -262,3 +262,15 @@ fn dont_lint_inside_macros() {
}
let _: Option<u32> = mac!(true, 42);
}
mod issue15770 {
fn maybe_error() -> Result<u32, &'static str> {
Err("error!")
}
pub fn trying(b: bool) -> Result<(), &'static str> {
let _x: Option<u32> = if b { Some(maybe_error()?) } else { None };
// Process _x locally
Ok(())
}
}