我有这样一个带有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));
}当我在函数中间使用它时,这个宏工作得很好,但当我在函数末尾使用它时,我会收到来自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)如何抑制此警告?我尝试将#[allow(clippy::needless_return)]添加到宏定义的最上面一行,但它不起作用。
发布于 2020-10-24 15:47:05
如果展开宏,则不需要最后一条return语句。
https://stackoverflow.com/questions/63798948
复制相似问题