rust-lang / rust-lang/rust-clippy

`clippy::multiple_unsafe_ops_per_block` and `unused_unsafe` not handled properly in nested unsafe calls/closures

Open
#13,134 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

I am calling an unsafe function, passing in a callback in a parameter, and inside that parameter I am calling another unsafe function. This works fine without touching any lints, but recently I enabled clippy::multiple_unsafe_ops_per_block (to combine it with clippy::undocumented_unsafe_blocks, but I think that's irrelevant to this bug), and it's emitting a warning that this unsafe block contains two unsafe operations. It does indeed... so I wrapped the inner one in an unsafe block. This warning doesn't go away from that, and causes rustc's builtin unused_unsafe lint to trigger.

I am not completely sure whether to report this here or if I should have done so on the compiler repo, since it involves a rustc builtin lint as well. This would probably require more changes than just a clippy change, I imagine.

Lint Name

clippy::multiple_unsafe_ops_per_block, and rustc's unused_unsafe

Reproducer

This code triggers clippy::multiple_unsafe_ops_per_block:

unsafe fn takes_closure(_: impl Fn()) {}
unsafe fn do_unsafe_op() {}

fn example() {
    // SAFETY: comment would be here
    unsafe {
        takes_closure(|| {
            do_unsafe_op()
        })
    }
}

This code still triggers the lint, but also triggers unused_unsafe

fn example() {
    // SAFETY: comment would be here
    unsafe {
        takes_closure(|| {
            // SAFETY: comment would be here
            unsafe { do_unsafe_op() }
        })
    }
}

I saw this happen:

warning: unnecessary `unsafe` block
  --> src/test.rs:33:13
   |
30 |     unsafe {
   |     ------ because it's nested under this `unsafe` block
...
33 |             unsafe { do_unsafe_op() }
   |             ^^^^^^ unnecessary `unsafe` block
   |
   = note: `#[warn(unused_unsafe)]` on by default

warning: this `unsafe` block contains 2 unsafe operations, expected only one
  --> src/test.rs:30:5
   |
30 | /     unsafe {
31 | |         takes_closure(|| {
32 | |             // SAFETY: comment would be here
33 | |             unsafe { do_unsafe_op() }
34 | |         })
35 | |     }
   | |_____^
   |
note: unsafe function call occurs here
  --> src/test.rs:31:9
   |
31 | /         takes_closure(|| {
32 | |             // SAFETY: comment would be here
33 | |             unsafe { do_unsafe_op() }
34 | |         })
   | |__________^
note: unsafe function call occurs here
  --> src/test.rs:33:22
   |
33 |             unsafe { do_unsafe_op() }
   |                      ^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#multiple_unsafe_ops_per_block
note: the lint level is defined here
  --> src/test.rs:6:5
   |
6  |     clippy::multiple_unsafe_ops_per_block,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I expected no warnings from this.

Side note... while writing this, I had a realisation, that I could refactor the closure expression to its own variable outside the unsafe block, and that would satisfy the lint. But I think this is still good to bring up anyways

Version
rustc 1.81.0-nightly (e9e6e2e44 2024-06-28)
binary: rustc
commit-hash: e9e6e2e444c30c23a9c878a88fbc3978c2acad95
commit-date: 2024-06-28
host: aarch64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the two Rust reproducer snippets and the clippy::multiple_unsafe_ops_per_block lint named in the report; no source file or test path is provided. Trace how nested closures and unsafe blocks are counted, including rustc's unused_unsafe interaction, and add coverage so the expected form produces no warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.