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

unsafe_op_in_unsafe_fn causes unnecessary unsafe warnings #132856

Open
ehuss opened this issue Nov 10, 2024 · 0 comments
Open

unsafe_op_in_unsafe_fn causes unnecessary unsafe warnings #132856

ehuss opened this issue Nov 10, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-edition-2024 Area: The 2024 edition A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-edition Diagnostics: An error or lint that should account for edition differences. D-papercut Diagnostics: An error or lint that needs small tweaks. L-false-positive Lint: False positive (should not have fired). L-unsafe_op_in_unsafe_fn Lint: unsafe_op_in_unsafe_fn T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ehuss
Copy link
Contributor

ehuss commented Nov 10, 2024

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
@ehuss ehuss added A-edition-2024 Area: The 2024 edition A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. D-edition Diagnostics: An error or lint that should account for edition differences. labels Nov 10, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 10, 2024
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-papercut Diagnostics: An error or lint that needs small tweaks. L-unsafe_op_in_unsafe_fn Lint: unsafe_op_in_unsafe_fn L-false-positive Lint: False positive (should not have fired). A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. C-bug Category: This is a bug. labels Nov 10, 2024
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 A-edition-2024 Area: The 2024 edition A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-edition Diagnostics: An error or lint that should account for edition differences. D-papercut Diagnostics: An error or lint that needs small tweaks. L-false-positive Lint: False positive (should not have fired). L-unsafe_op_in_unsafe_fn Lint: unsafe_op_in_unsafe_fn T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants