Skip to content

Erroneous unused_unsafe diagnostic for macro invocations #94912

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

Open
zdimension opened this issue Mar 13, 2022 · 0 comments
Open

Erroneous unused_unsafe diagnostic for macro invocations #94912

zdimension opened this issue Mar 13, 2022 · 0 comments
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

@zdimension
Copy link

zdimension commented Mar 13, 2022

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

unsafe fn unsafe_code() { }

macro_rules! macro_inner
{
    () => { unsafe { unsafe_code(); }  };
}
macro_rules! macro_outer
{
    () => { unsafe { macro_inner!(); unsafe_code(); } };
}

fn main()
{
    macro_inner!();

    macro_outer!();
}

The current output is:

warning: unnecessary `unsafe` block
  --> src\poc.rs:5:13
   |
5  |     () => { unsafe { unsafe_code(); }  };
   |             ^^^^^^ unnecessary `unsafe` block
...
9  |     () => { unsafe { macro_inner!(); unsafe_code(); } };
   |             ------ because it's nested under this `unsafe` block
...
16 |     macro_outer!();
   |     -------------- in this macro invocation
   |
   = note: this warning originates in the macro `macro_inner` (in Nightly builds, run with -Z macro-backtrace for more info)

This is obviously wrong, because macro_inner! is also invoked at line 14, in the main function, outside an unsafe block. The analyzer seems to only take in account the invocation occurring inside the macro_outer! expansion during the unused_unsafe check.

Although it makes technical sense to perform the analysis that way, the result is that an incorrect piece of advice is given to the user ("This unsafe specifier is unnecessary, remove it"). Here, removing the unsafe block breaks the code, for the first invocation.

Tested on stable 1.58.1 and nightly 1.61.0 (2022-02-23).

EDIT: still present on 1.67.0 (2022-11-02).

PS: Sorry if this is not in the right triage category, I wasn't sure whether it would better fit the "Diagnostics" or the "Bug" label.

@zdimension zdimension 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 Mar 13, 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

No branches or pull requests

1 participant