-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#[must_use] does not work for nested structures #39524
Comments
This definitely comes up with |
NOTE: This is another breaking change that will be part of the 2.0.0 release. This diff replaces weakly typed `String`s, `Url`s, and other types with new types generated using the `NewType` pattern. Using stronger types here should avoid common mistakes (e.g., switching the order of the authorization and endpoint URLs when instantiating a new `Client`). In addition to adding a `NewType` trait, this diff adds a `NewSecretType` trait, which implements `Debug` in a way that redacts the secret. This behavior avoids a common source of security bugs: logging secrets, especially when errors occur. Unlike the `NewType` trait, the `NewSecretType` does not implement `Deref`. Instead, the secret must be explicitly extracted by calling the `secret` method. Finally, this PR resolves #28 by having the `authorize_url` method accept a closure for generating a fresh CSRF token on each invocation. The token is returned by the method as `#[must_use]`, which the caller should compare against the response sent by the authorization server to the redirect URI. Note that `#[must_use]` currently has no effect in this context, but it should once rust-lang/rust#39524 is resolved.
I think this should essentially be considered a straightforward extension of #61061 (which extended |
Extend `#[must_use]` to nested structures Extends the `#[must_use]` lint to apply when `#[must_use]` types are nested within `struct`s (or one-variant `enum`s), making the lint much more generally useful. This is in line with #61100 extending the lint to tuples. Fixes #39524. cc @rust-lang/lang and @rust-lang/compiler for discussion in case this is a controversial change. In particular, we might want to consider allowing annotations on fields containing `#[must_use]` types in user-defined types (e.g. `#[allow(unused_must_use)]`) to opt out of this behaviour, if there are cases where we this this is likely to have frequent false positives. (This is based on top of #62235.)
Extend `#[must_use]` to nested structures Extends the `#[must_use]` lint to apply when `#[must_use]` types are nested within `struct`s (or one-variant `enum`s), making the lint much more generally useful. This is in line with #61100 extending the lint to tuples. Fixes #39524. cc @rust-lang/lang and @rust-lang/compiler for discussion in case this is a controversial change. In particular, we might want to consider allowing annotations on fields containing `#[must_use]` types in user-defined types (e.g. `#[allow(unused_must_use)]`) to opt out of this behaviour, if there are cases where we this this is likely to have frequent false positives. (This is based on top of #62235.)
Extend `#[must_use]` to nested structures Extends the `#[must_use]` lint to apply when `#[must_use]` types are nested within `struct`s (or one-variant `enum`s), making the lint much more generally useful. This is in line with #61100 extending the lint to tuples. Fixes #39524. cc @rust-lang/lang and @rust-lang/compiler for discussion in case this is a controversial change. In particular, we might want to consider allowing annotations on fields containing `#[must_use]` types in user-defined types (e.g. `#[allow(unused_must_use)]`) to opt out of this behaviour, if there are cases where we this this is likely to have frequent false positives. (This is based on top of #62235.)
Extend `#[must_use]` to nested structures Extends the `#[must_use]` lint to apply when `#[must_use]` types are nested within `struct`s (or one-variant `enum`s), making the lint much more generally useful. This is in line with #61100 extending the lint to tuples. Fixes #39524. cc @rust-lang/lang and @rust-lang/compiler for discussion in case this is a controversial change. In particular, we might want to consider allowing annotations on fields containing `#[must_use]` types in user-defined types (e.g. `#[allow(unused_must_use)]`) to opt out of this behaviour, if there are cases where we this this is likely to have frequent false positives. (This is based on top of #62235.)
Extend `#[must_use]` to nested structures Extends the `#[must_use]` lint to apply when `#[must_use]` types are nested within `struct`s (or one-variant `enum`s), making the lint much more generally useful. This is in line with #61100 extending the lint to tuples. Fixes #39524. cc @rust-lang/lang and @rust-lang/compiler for discussion in case this is a controversial change. In particular, we might want to consider allowing annotations on fields containing `#[must_use]` types in user-defined types (e.g. `#[allow(unused_must_use)]`) to opt out of this behaviour, if there are cases where we this this is likely to have frequent false positives. (This is based on top of #62235.)
Closed as not being desirable in #62262 (comment). We would need a more nuanced handling of |
I see this was rejected in general, but would it be doable to special case it for |
@jebrosen: that should be possible — could you open a new issue for this? I'll try to write up some mentoring advice. |
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)
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
The text was updated successfully, but these errors were encountered: