17

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);
}
mq7
  • 1,125
  • 2
  • 11
  • 21
  • 1
    I believe your question is answered by the answers of [How to disable unused code warnings in Rust?](https://stackoverflow.com/q/25877285/155423). TL;DR: `let _ = foo()`. If you disagree, please [edit] your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster May 20 '18 at 16:31
  • 7
    I believe this question is incorrectly marked as a duplicate of two other questions. It is related only in the sense that they are about silencing warnings. This question is specifically about a case for which a single line `allow` does not work. No answer in the other questions addresses this. – Robert Jacobson Aug 29 '20 at 22:29
  • At time of writing, you still have to put a single statement in braces after the `#[allow(...)]`. As a one-line alternative, you can do `if std::fs::remove_file(&path1).is_err() {}`. – BallpointBen May 04 '23 at 16:33

0 Answers0