Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hint at using unsafe_op_in_unsafe_fn in the unused_unsafe warning #90776

Closed
seritools opened this issue Nov 10, 2021 · 1 comment · Fixed by #93678
Closed

Hint at using unsafe_op_in_unsafe_fn in the unused_unsafe warning #90776

seritools opened this issue Nov 10, 2021 · 1 comment · Fixed by #93678
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@seritools
Copy link
Contributor

seritools commented Nov 10, 2021

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

@seritools seritools added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 10, 2021
@steffahn
Copy link
Member

steffahn commented Feb 6, 2022

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”.

@bors bors closed this as completed in 45e2c28 Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants