Skip to content

Explicit warning on nested unsafe blocks #39144

Closed
@Wilfred

Description

@Wilfred

When refactoring code, it's easy to accidentally end up with nested unsafe blocks. This can lead to confusing rustc errors.

unsafe fn very_dangerous() -> u64 {
    1
}

fn main() {
    unsafe {
        // lots of code...
        
        unsafe {
            very_dangerous();
        }
    }

The compiler says:

rustc 1.16.0-nightly (ff591b6dc 2017-01-15)
warning: unnecessary `unsafe` block, #[warn(unused_unsafe)] on by default
  --> <anon>:9:9
   |
9  |           unsafe {
   |  _________^ starting here...
10 | |             very_dangerous();
11 | |         }
   | |_________^ ...ending here

This is surprising at first glance, since very_dangerous requires an unsafe block but the message implies otherwise. Could we warn about nested unsafe blocks?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsE-help-wantedCall for participation: Help is requested to fix this issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions