I can use the #[must_use]
attribute to mark a function as having a return value that must either be used or explicitly ignored:
#[must_use]
fn plain() -> i32 {
1
}
fn main() {
plain(); // warning
}
However, if I want to change my function so it now returns a Result
, I cannot do this:
#[must_use]
fn result() -> Result<i32, ()> {
Ok(1)
}
fn main() {
result().unwrap();
}
because the call to .unwrap()
counts as a "use", and the #[must_use]
attribute is applying to the entire Result
.
Is there a way to make #[must_use]
apply to the inner type as well? Essentially I would like to make sure that the i32
is not accidentally "eaten" by the semicolon.
I have the additional constraint that I cannot change the public API of this function, so I can't make it return a Result<MustUse<i32>, ()>
or something along those lines.