-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Warn about blanket let _ =
#6842
Comments
Interesting idea. I like this as an answer to "What's the idiomatic way to drop a must_use value?" I believe we should implement this by enhancing the I like your suggestion more than the alternatives. But a |
I would like to think that it is, I couldn't come up with a counterexample myself. Maybe someone more familiar with async could chime in. |
I like See also rust-lang/rust#66116 |
Just chiming in that I just ran into this an our codebase with a future whose result I didn't care about. We did a let binding let _ = update_status_but_its_okay_to_fail(); But since the let binding swallows the must_use on the future, we never got the proper lint about futures needing to be polled. It would be great to have at the bare minimum, a lint |
@rustbot claim |
Downgrade let_underscore_untyped to restriction From reading #6842 I am not convinced of the cost/benefit of this lint even as a pedantic lint. It sounds like the primary motivation was to catch cases of `fn() -> Result` being changed to `async fn() -> Result`. If the original Result was ignored by a `let _`, then the compiler wouldn't guide you to add `.await`. **However, this situation is caught in a more specific way by [let_underscore_future](https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_future) which was introduced _after_ the original suggestion (#9760).** In #10410 it was mentioned twice that a <kbd>restriction</kbd> lint might be more appropriate for let_underscore_untyped. changelog: Moved [`let_underscore_untyped`] to restriction
What it does
What does this lint do?
Suggest replacing
let _ = <expr>
withlet _: <type> = <expr>
Categories (optional)
clippy::pedantic
What is the advantage of the recommended code over the original code
It's somewhat common to use
let _ = <expr>
to ignore results that would otherwise generate a warning when not used. e.g. ignore an unimportant error.However, if changes elsewhere cause the type of
<expr>
to change, bugs could be silently introduced, aslet _ =
would accept any type.For example,
<expr>
might go fromResult<_>
toimpl Future<Output=Result<_>>
, because an upstream crate changed a function toasync fn
. And this could cause some operation to never be performed.Asking the user to always specify the type in
let _ =
would avoid this kind of bugs.Drawbacks
Slightly more verbose code.
Example
Could be written as:
Alternatives
impl Future
is dropped inlet _ =
, it almost never makes sense to construct a future then immediately drop it.<expr>.ok()
instead oflet _ = <expr>
, for the case where<expr>
isResult<_, _>
Either option works for the example above, but I think my suggestion is more general.
The text was updated successfully, but these errors were encountered: