Jason
Jason

Reputation: 1

How to disable the "unused code must be used" warning from macro?

I tried to add allow dead_code and unused_must_use:

#[allow(dead_code)]
#[allow(unused_must_use)]
#[implement(MyStruct)]
pub struct MyStructList(pub Rc<Vec<MyStruct>>);

But still got the warning, still new to rust, what does it mean to call drop ?

warning: unused return value of `Box::<T>::from_raw` that must be used
  --> test.rs
   |
   | #[implement(MyStruct)]
   | ^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: call `drop(from_raw(ptr))` if you intend to drop the `Box`
   = note: this warning originates in the attribute macro `implement` (in Nightly builds, run with -Z macro-backtrace for more info)

Upvotes: 0

Views: 992

Answers (1)

rodrigo
rodrigo

Reputation: 98516

I had this issue once, with a macro of my own. At the end I fixed the macro, but while the bug was there I workarounded with this trick:

#[allow(unused_must_use)]
mod my_struct {
    use super::*;
    #[implement(MyStruct)]
    pub struct MyStructList(pub Rc<Vec<MyStruct>>);
}
pub use my_struct::*;

This is similar to the solution by @Andrew, but the allow directive applies only to the inner private module, instead of to all your module.

You may need to add/fix the pub use below depending on the details of what your macro exactly does. You may prefer pub use my_struct::MyStructList;, for example, and have finer control of your exports.

Upvotes: 2

Related Questions