[英]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));
}
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.