Skip to content

FP in clippy::if_then_some_else_none with short-circuiting ? at sole expression #15770

@matrach

Description

@matrach

Summary

This seems like a continuation of #11394, but the case is even simpler with ? at the tail expression.

Lint Name

if_then_some_else_none

Reproducer

I tried this code playground link:

#![warn(clippy::if_then_some_else_none)]

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

Clippy generated an invalid suggestion:

warning: this could be simplified with `bool::then`
  --> src/lib.rs:10:26
   |
10 |       let _: Option<u32> = if b {
   |  __________________________^
11 | |         Some(maybe_error()?)
12 | |     } else {
13 | |         None
14 | |     };
   | |_____^ help: try: `b.then(|| maybe_error()?)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_then_some_else_none
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
 1 | #![warn(clippy::if_then_some_else_none)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

resulting in a compilation error:

    Checking playground v0.0.1 (/playground)
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `std::ops::FromResidual`)
  --> src/lib.rs:40:49
   |
40 |     let _: Option<u32> = b.then(|| maybe_error()?);
   |                                 --              ^ cannot use the `?` operator in a closure that returns `u32`
   |                                 |
   |                                 this function should return `Result` or `Option` to accept `?`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 1 previous error

Version

Same effect with playground versions:

Stable channel
Build using the Stable version: 1.90.0


Beta channel
Build using the Beta version: 1.91.0-beta.3
(2025-09-20 bb624dcb4c8ab987e10c)


Nightly channel
Build using the Nightly version: 1.92.0-nightly
(2025-09-26 54a8a1db604e4caff93e)

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions