너를 *_*속였다 9 compiler-warnings rust clippy
我有一个带有 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));
}
Run Code Online (Sandbox Code Playgroud)
当我在函数中间使用它时,这个宏工作正常,但是当我在函数末尾使用它时,我收到来自 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)
Run Code Online (Sandbox Code Playgroud)
我怎样才能抑制这个警告?我尝试添加#[allow(clippy::needless_return)]到宏定义的上一行,但没有用。