Skip to content

unsafe{} when used in macros can emit "unnecessary unsafe block" warning without macro invocation location #32747

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

Closed
codyps opened this issue Apr 5, 2016 · 1 comment

Comments

@codyps
Copy link
Contributor

codyps commented Apr 5, 2016

The warning itself could be resolved by stmt_expr_attributes (once it is stable) by marking the inner unsafe{} as OK to be unneeded. (If we don't stabilize those, though, we may want to consider whether applying the unused_unsafe warning to unsafe{} generated by macros makes sense.

In any case, we should get an indication of where the macro expansion causing the error is occurring (right now only the macro location is printed, not the invocation location).

Tested with rustc 1.9.0-nightly (600dc3552 2016-04-04)

Example:


macro_rules! another_unsafe {
    ($e:expr) => ({
        unsafe { $e }
    })
}

fn main() {
    let x : usize = unsafe { another_unsafe!(std::mem::transmute(3usize)) };
    println!("{:?}", x);
}

Output:

src/main.rs:4:9: 4:22 warning: unnecessary `unsafe` block, #[warn(unused_unsafe)] on by default
src/main.rs:4         unsafe { $e }
                      ^~~~~~~~~~~~~

Expected: some complaint about line 9 (invocation of another_unsafe!)

@Mark-Simulacrum
Copy link
Member

I believe this is fixed:

warning: unnecessary `unsafe` block
 --> test.rs:4:9
  |
4 |         unsafe { $e }
  |         ^^^^^^^^^^^^^ unnecessary `unsafe` block
...
9 |     let x : usize = unsafe { another_unsafe!(std::mem::transmute(3usize)) };
  |                              -------------------------------------------- in this macro invocation
  |
  = note: #[warn(unused_unsafe)] on by default
note: because it's nested under this `unsafe` block
 --> test.rs:9:21
  |
9 |     let x : usize = unsafe { another_unsafe!(std::mem::transmute(3usize)) };
  |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants