Open
Description
#[must_use]
has a hole in its coverage. Suppose we have some code like this:
let transaction = Transaction::new(&kitchen)
.catch_frogs(3)
.set_gas_flow(2.5);
if transaction.is_boiling() {
println!("How I love a nice bowl of frog soup!");
}
At this point, #[must_use]
is satisfied merely because transaction
has been inspected (via &self
), but the call to transaction.commit()
has been forgotten and there may be an error to rollback. What we want is for there to be a warning if the type has not been properly disposed of.
A type can be disposed of by destructuring, or by passing it off to another function.
Some consequences:
mem::drop
counts as disposal. It won't be infallible for the same reasons that Drop isn't infallible.- Traits like
trait Foo { fn foo(self) {} }
would expose a hole. And it could be a blanket implementation. - A method can ignore
self
by usinglet Self { .. } = self
.
Result
could be marked #[must_use(dispose)]
. But I don't think it should be.
Alternatives
- Do nothing, and change
fn inspect(&self) -> bool
tofn inspect(self) -> (Self, bool)
. - Do nothing.
- Implement with clippy instead.
- A different syntax, like
#[must_dispose]
. - Make
#[must_use]
function like the proposed#[must_use(dispose)]
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment