Skip to content

Hint at using unsafe_op_in_unsafe_fn in the unused_unsafe warning #90776

@seritools

Description

@seritools
Contributor

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 :)

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Nov 10, 2021
steffahn

steffahn commented on Feb 6, 2022

@steffahn
Member

I’ve created a PR that would generate the following output:

warning: unnecessary `unsafe` block
 --> src/main.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: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn`
  = note: `#[allow(unsafe_op_in_unsafe_fn)]` on by default

(The note: `#[allow(unsafe_op_in_unsafe_fn)]` on by default part – technically – just explains where the lint-level for the first unsafe operation inside the block for which unsafe_op_in_unsafe_fn is allowed comes from, in the same style as it’s done e.g. for the unused_unsafe warning itself in the other, earlier, note.)

Feel free to give feedback on this output, or ask questions. I do suppose it fulfills the main property to “hint at unsafe_op_in_unsafe_fn even being a thing”.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @steffahn@seritools

      Issue actions

        Hint at using `unsafe_op_in_unsafe_fn` in the `unused_unsafe` warning · Issue #90776 · rust-lang/rust