Skip to content

Commit 3ca0738

Browse files
authored
Fix if_then_some_else_none FP when return exists in block expr (#15783)
Closes #15770 changelog: [`if_then_some_else_none`] fix FP when return exists in block expr
2 parents 189b4c3 + 642feb7 commit 3ca0738

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

clippy_lints/src/if_then_some_else_none.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
7979
&& !is_in_const_context(cx)
8080
&& self.msrv.meets(cx, msrvs::BOOL_THEN)
8181
&& !contains_return(then_block.stmts)
82+
&& then_block.expr.is_none_or(|expr| !contains_return(expr))
8283
{
8384
let method_name = if switch_to_eager_eval(cx, expr) && self.msrv.meets(cx, msrvs::BOOL_THEN_SOME) {
8485
sym::then_some

tests/ui/if_then_some_else_none.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,15 @@ fn dont_lint_inside_macros() {
206206
}
207207
let _: Option<u32> = mac!(true, 42);
208208
}
209+
210+
mod issue15770 {
211+
fn maybe_error() -> Result<u32, &'static str> {
212+
Err("error!")
213+
}
214+
215+
pub fn trying(b: bool) -> Result<(), &'static str> {
216+
let _x: Option<u32> = if b { Some(maybe_error()?) } else { None };
217+
// Process _x locally
218+
Ok(())
219+
}
220+
}

tests/ui/if_then_some_else_none.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,15 @@ fn dont_lint_inside_macros() {
262262
}
263263
let _: Option<u32> = mac!(true, 42);
264264
}
265+
266+
mod issue15770 {
267+
fn maybe_error() -> Result<u32, &'static str> {
268+
Err("error!")
269+
}
270+
271+
pub fn trying(b: bool) -> Result<(), &'static str> {
272+
let _x: Option<u32> = if b { Some(maybe_error()?) } else { None };
273+
// Process _x locally
274+
Ok(())
275+
}
276+
}

0 commit comments

Comments
 (0)