Skip to content

Macros can't hide unsafety because of unused-unsafe #8472

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
bblum opened this issue Aug 12, 2013 · 4 comments
Closed

Macros can't hide unsafety because of unused-unsafe #8472

bblum opened this issue Aug 12, 2013 · 4 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-syntaxext Area: Syntax extensions

Comments

@bblum
Copy link
Contributor

bblum commented Aug 12, 2013

Consider the following.

macro_rules! foo(
        () => {
            unsafe { use std::cast; let () = cast::transmute(()); }
        }   
)
fn main() {
    unsafe { foo!(); }
    foo!();
}

This only does not emit a warning if the first line of code in main is removed. It would be nice to be able to squelch the unused unsafe warning that appears on the first invocation of foo!(), whether automatically or by being able to use #[allow(unused_unsafe)] inside the macro definition.

A workaround is to define a function in the macro that hides the unsafety and to call it.

@treeman
Copy link
Contributor

treeman commented Aug 14, 2014

Still relevant

$ rustc -v
rustc 0.12.0-pre (470dbef29 2014-07-25 09:31:10 +0000)
#![feature(macro_rules)]

macro_rules! foo(
    () => {
        unsafe { use std::mem; let () = mem::transmute(()); }
    }
)
fn main() {
    unsafe { foo!(); }
    foo!();
}

Produces warning: unnecessary unsafe block.

It is possible to avoid the warning with

fn main() {
    #![allow(unused_unsafe)]
    unsafe { foo!(); }
    foo!();
}

But not within the macro definition.

@iliekturtles
Copy link
Contributor

Another workaround is to have multiple match patterns. This pattern may cause confusion since the unsafe at the macro invocation is part of the macro syntax and doesn't guarantee anything about the macro itself.

macro_rules! foo {
    (unsafe $e:expr) => {
        unsafe { foo!($e) }
    };
    ($e:expr) => {{
       use std::mem;
       mem::transmute($e)
    }}
}
#[allow(unused_variables)]
fn main() {
    let x: u32 = unsafe { foo!(1) };
    let y: u32 = foo!(unsafe 1);
}

@mitaa
Copy link
Contributor

mitaa commented Dec 11, 2015

This just works now

#![feature(stmt_expr_attributes)]

macro_rules! foo(
        () => {
            #[allow(unused_unsafe)]
            unsafe { use std::mem; let () = mem::transmute(()); }
        }   
);

fn main() {
    unsafe { foo!(); }
    foo!();
}

@leoyvens
Copy link
Contributor

leoyvens commented May 7, 2017

Triage: This is fixed as noted above, stmt_expr_attributes is stable now.

flip1995 pushed a commit to flip1995/rust that referenced this issue Feb 26, 2022
…t_should_not_trigger_on_doc_hidden_items, r=flip1995

Disable ``[`new-without-default`]`` for new() methods that are marked…

… with '#[doc(hidden)]'

Fixes rust-lang#8152

changelog: Disable ``[`new-without-default`]`` for new() methods that are marked with `#[doc(hidden)]`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-syntaxext Area: Syntax extensions
Projects
None yet
Development

No branches or pull requests

6 participants