Description
When #[must_use]
is applied to a structure that is nested, the linter no longer picks up when that value is unused.
This is related to: #26291 and #26281
Looking at those issues, there seem to be a lot of cases where #[must_use]
does not work. This is unfortunate because it is actually an incredibly useful annotation that definitely has applications past just the Result enum.
Accidentally relying on it too much can have you make mistakes you otherwise would have corrected had the warning been a bit smarter. I consider this a bug because #[must_use]
implies that you must use that value no matter what; regardless of what form it is returned in.
I tried this code: (Rust Playground: https://is.gd/vCLZKq)
#[must_use]
#[derive(Debug)]
struct MustUse;
struct Foo {
must: MustUse,
}
impl Foo {
fn bar(&self) {
println!("{:?}", self.must);
}
}
fn must() -> Foo {
Foo {must: MustUse}
}
fn main() {
must().bar();
must();
}
I expected to see this happen: A warning about the MustUse structure not being used
Instead, this happened: The code compiled and ran with no errors
Meta
rustc --version --verbose
: stable, nightly, etc.
Backtrace: not applicable