Skip to content

Warn about blanket let _ =  #6842

Closed
@yshui

Description

@yshui

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

  1. Warn when an impl Future is dropped in let _ =, it almost never makes sense to construct a future then immediately drop it.
  2. Suggest using <expr>.ok() instead of let _ = <expr>, for the case where <expr> is Result<_, _>

Either option works for the example above, but I think my suggestion is more general.

Metadata

Metadata

Assignees

Labels

C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesS-needs-discussionStatus: Needs further discussion before merging or work can be startedgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions