Skip to content

Hint at using unsafe_op_in_unsafe_fn in the unused_unsafe warningΒ #90776

Closed
@seritools

Description

@seritools

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7d4fd2e742e8d92ddc1032a8328f95b2

unsafe fn foo() -> u32 {
    unsafe {
        std::mem::transmute::<i32, u32>(5)
    }
}

The current output is:

warning: unnecessary `unsafe` block
 --> src/lib.rs:2:5
  |
1 | unsafe fn foo() -> u32 {
  | ---------------------- because it's nested under this `unsafe` fn
2 |     unsafe {
  |     ^^^^^^ unnecessary `unsafe` block
  |
  = note: `#[warn(unused_unsafe)]` on by default

Ideally the output should look like (very roughly):

warning: unnecessary `unsafe` block
 --> src/lib.rs:2:5
  |
1 | unsafe fn foo() -> u32 {
  | ---------------------- because it's nested under this `unsafe` fn
2 |     unsafe {
  |     ^^^^^^ unnecessary `unsafe` block
  |
  = note: `#[warn(unused_unsafe)]` on by default
  = note: use `#[warn(unsafe_op_in_unsafe_fn)]` to allow unsafe blocks in unsafe functions in places where they would be needed in a safe function

The note should only appear when it's an appropriate usage of an unsafe block, i.e. the block's contents aren't safe.
This would hint at unsafe_op_in_unsafe_fn even being a thing right from the output :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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