Skip to content

manual_let_else should surround unsafe {} with brackets (). #15914

@kristof-mattei

Description

@kristof-mattei

Summary

Clippy suggests code that does not compile.

Lint Name

manual_let_else

Reproducer

I tried this code:

unsafe fn something_unsafe() -> Option<u32> {
    None
}

fn main() {
    let value = if let Some(value) = unsafe { something_unsafe() } {
        value
    } else {
        return;
    };

    println!("{}", value);
}

I saw this happen:

warning: this could be rewritten as \`let...else\`
  --> src/main.rs:6:5
   |
 6 | /     let value = if let Some(value) = unsafe { something_unsafe() } {
 7 | |         value
 8 | |     } else {
 9 | |         return;
10 | |     };
   | |______^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_let_else
   = note: `-W clippy::manual-let-else` implied by `-W clippy::pedantic`
   = help: to override `-W clippy::pedantic` add `#[allow(clippy::manual_let_else)]`
help: consider writing
   |
 6 ~     let Some(value) = unsafe { something_unsafe() } else {
 7 +         return;
 8 +     };
   |

I expected to see this happen:

Nothing, I wouldn't expect the lint to trigger.

Accepting the lint's suggestion:

let Some(value) = unsafe { something_unsafe() } else {
    return;
};

Fails to compile:

error: right curly brace `}` before `else` in a `let...else` statement not allowed
 --> src/main.rs:6:51
  |
6 |     let Some(value) = unsafe { something_unsafe() } else {
  |                                                   ^
  |
help: wrap the expression in parentheses
  |
6 |     let Some(value) = (unsafe { something_unsafe() }) else {
  |                       +                             +

Subsequently wrapping it does work.

Version

❯ cargo clippy --version
clippy 0.1.90 (1159e78c47 2025-09-14)

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions