【问题标题】:How to disable the "unused code must be used" warning from macro?如何禁用宏中的“必须使用未使用的代码”警告?
【发布时间】:2022-09-23 12:39:45
【问题描述】:

我尝试添加允许dead_codeunused_must_use

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

但是仍然收到警告,仍然是新的生锈,调用 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)

标签: rust compiler-warnings rust-proc-macros


【解决方案1】:

我曾经遇到过这个问题,我自己有一个宏。最后我修复了宏,但是当错误存在时,我用这个技巧解决了这个问题:

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

这类似于@Andrew 的解决方案,但allow 指令仅适用于内部私有模块,而不适用于您的所有模块。

您可能需要添加/修复下面的pub use,具体取决于您的宏的具体功能。例如,您可能更喜欢pub use my_struct::MyStructList;,并且可以更好地控制您的出口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 2023-03-12
    • 2016-04-30
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    相关资源
    最近更新 更多