rust-lang / rust-lang/rust

unsafe_op_in_unsafe_fn causes unnecessary unsafe warnings

Open
#132,856 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-edition-2024 A-lints A-suggestion-diagnostics D-edition D-papercut I-edition-triaged L-false-positive L-unsafe_op_in_unsafe_fn T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#![warn(unsafe_op_in_unsafe_fn)]
pub unsafe fn f() {
    let _ = std::mem::zeroed::<i16>();
    unsafe {
        let _ = std::mem::zeroed::<i32>();
    }
}

This causes a diagnostic suggestion to rewrite it to:

#![warn(unsafe_op_in_unsafe_fn)]
pub unsafe fn f() { unsafe {
    let _ = std::mem::zeroed::<i16>();
    unsafe {
        let _ = std::mem::zeroed::<i32>();
    }
}}

However, this in turn causes more warnings which cannot be auto-fixed:

warning: unnecessary `unsafe` block
 --> src/main.rs:4:5
  |
2 | pub unsafe fn f() { unsafe {
  |                     ------ because it's nested under this `unsafe` block
3 |     let _ = std::mem::zeroed::<i16>();
4 |     unsafe {
  |     ^^^^^^ unnecessary `unsafe` block
  |
  = note: `#[warn(unused_unsafe)]` on by default

I don't know if it would be possible to change unused_unsafe to have a machine-applicable suggestion to remove the unsafe keyword. Or maybe unsafe_op_in_unsafe_fn could incorporate those suggestions. I think it probably should not remove the brackets, since that would have a semantic change, and I think would be difficult to get right (though could actually help with some of the problems of the tail-drop-order changes).

Priority-wise, this is just an annoyance since the warnings do not inhibit migration. They just need to be cleaned up manually which for a large codebase could be a lot of work.

Meta

rustc --version --verbose:

rustc 1.84.0-nightly (59cec72a5 2024-11-08)
binary: rustc
commit-hash: 59cec72a57af178767a7b8e7f624b06cc50f1087
commit-date: 2024-11-08
host: aarch64-apple-darwin
release: 1.84.0-nightly
LLVM version: 19.1.3

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 by reproducing the warning and suggested rewrite from the issue's Rust snippet on the reported nightly compiler. Trace the unsafe_op_in_unsafe_fn and unused_unsafe diagnostic handling in rustc, then verify that the resulting suggestion is machine-applicable without introducing additional warnings.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.