简体   繁体   English

如何抑制来自宏的 Clippy 警告?

[英]How can I suppress a Clippy warning originating from a macro?

I have macro with return statement like this:我有一个带有 return 语句的宏,如下所示:

macro_rules! return_fail {
    ( $res:expr ) => {
        match $res {
            Ok(val) => val,
            Err(e) => {
                eprintln!(
                    "An error: on {}:{} {}; aborting current function.",
                    file!(),
                    line!(),
                    e
                );
                return;
            }
        }
    };
}

fn bad(flag: bool) -> Result<(), String> {
    if flag {
        Ok(())
    } else {
        Err("u r idiot".to_string())
    }
}

fn main() {
    return_fail!(bad(true));
    return_fail!(bad(false));
}

Rust playground 铁锈游乐场

This macro works fine when I use it in the middle of a function, but when I use it at the end of the function I get a warning from Clippy:当我在函数中间使用它时,这个宏工作正常,但是当我在函数末尾使用它时,我收到来自 Clippy 的警告:

warning: unneeded `return` statement
  --> src/main.rs:12:17
   |
12 |                 return;
   |                 ^^^^^^^ help: remove `return`
...
28 |     return_fail!(bad(false));
   |     ------------------------- in this macro invocation
   |
   = note: `#[warn(clippy::needless_return)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
   = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

How I can suppress this warning?我怎样才能抑制这个警告? I tried adding #[allow(clippy::needless_return)] to the upper line of macro definition but it did not work.我尝试将#[allow(clippy::needless_return)]到宏定义的上一行,但没有用。

如果您展开宏,则不需要最后一个 return 语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复针对 or_insert / or_insert_with 的 clippy::or_fun_call 的 Clippy 警告? - How do I fix the Clippy warning for clippy::or_fun_call for or_insert / or_insert_with? 在 Rust 中使用 clippy 如何禁用 clippy::struct-excessive-bools? - In Rust using clippy how can I disable clippy::struct-excessive-bools? 如何删除 Rust clippy 警告`考虑使用`vec:[]`宏:`let mut https: Vec<u8> = vec.[.;];`</u8> - How to remove Rust clippy warning `consider using the `vec![]` macro: `let mut https: Vec<u8> = vec![..];` 使用 Substrate 的 decl_event 时,如何隐藏 Clippy 关于“不需要的单元表达式”的警告? - How do I hide the warning from Clippy about "unneeded unit expression" when using Substrate's decl_event? 如何修复此 clippy 警告(无需收集) - How to fix this clippy warning (needless collect) 如何为工作区中的所有板条箱共享 Clippy 配置? - How can I have a shared Clippy configuration for all the crates in a workspace? 如何阻止`cargo clippy` 也运行依赖项? - How to stop `cargo clippy` from running on dependencies as well? 如何防止 cargo clippy 分析生成的 prost 文件 - How to prevent cargo clippy from analyizing generated prost files 如何从 rust 宏返回格式化字符串 - How can i return a formatted string from a rust macro 如何从 rust 宏中的“Foo::Bar”中获取“Bar”? - How can I get “Bar” from “Foo::Bar” in rust macro?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM