I have lines that return Result
. The result does not matter and I don't want unwrap()
or any logging. (The frequent failures are expected)
How can I silence the unused warnings for only these lines?
#[allow(unused_must_use)]
seems to work when applied to fn level, but this does not work:
#[allow(unused_must_use)]
std::fs::remove_file(&path1);
#[allow(unused_must_use)]
std::fs::remove_file(&path2);
Edit:
while let _ = ..
works as a workaround, #[allow(unused..)]
specifically exists for this case, and the compiler also suggests to use it. Reading let _ = ..
adds another layer of thinking. (assign and then abandon) So I prefer #[allow(..)]
although it is more verbose. (If I see let _ = ..
frequent enough and I get used to it, I may change my preference)
So I searched around and found some codes that apply macros to the statement level.
This question asks for why #[allow(unused_must_use)]
for a line doesn't work-- wrong syntax? bug? just unimplemented yet for unused_must_use
?
Edit: According to this:
How to quiet a warning for a single statement in Rust? My code should work.
Reading these:
https://github.com/rust-lang/rust/issues/36675
https://github.com/rust-lang/rust/issues/15701
I tried the allow on the scope level and it worked.
#[allow(unused_must_use)] {
std::fs::remove_file(&path1);
std::fs::remove_file(&path2);
}