Description
What it does
What does this lint do?
Suggest replacing let _ = <expr>
with let _: <type> = <expr>
Categories (optional)
- Kind: Tentatively
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, as let _ =
would accept any type.
For example, <expr>
might go from Result<_>
to impl Future<Output=Result<_>>
, because an upstream crate changed a function to async 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
let _ = do_something_and_ignore_error();
Could be written as:
let _: Result<_, _> = do_something_and_ignore_error();
Alternatives
- Warn when an
impl Future
is dropped inlet _ =
, it almost never makes sense to construct a future then immediately drop it. - Suggest using
<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.