Skip to content

option_map_unit_fn incorrectly removes unsafe #15568

@VorpalBlade

Description

@VorpalBlade

Summary

Note this silly code (which I came across in the wild!):

// self.program_id is an Option<u32>
self.program_id.map(|pid| unsafe {
    libc::kill(pid as i32, libc::SIGTERM);
});

Clippy suggests (and with --fix tries to apply):

warning: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
   --> src/tracer.rs:113:9
    |
113 | //         self.program_id.map(|pid| unsafe {
114 | ||             libc::kill(pid as i32, libc::SIGTERM);
115 | ||         });
    | ||__________^- help: try: `if let Some(pid) = self.program_id { libc::kill(pid as i32, libc::SIGTERM); }`
    |  |__________|
    |
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unit_fn

This does not compile as the unsafe block is lost.

Reproducer

I tried this code:

fn main() {
    let x = Some(1u32);
    x.map(|pid| unsafe { libc::kill(pid as i32, libc::SIGHUP); });
}

I expected to see this happen:

if let Some(pid) = x { unsafe { libc::kill(pid as i32, libc::SIGHUP); } }

Instead, this happened:

if let Some(pid) = x { libc::kill(pid as i32, libc::SIGHUP); }

Version

rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: x86_64-unknown-linux-gnu
release: 1.89.0
LLVM version: 20.1.7

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-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