Skip to content

detect unusable bindings in arguments to matches! #7351

Closed
@Fishrock123

Description

@Fishrock123

I opened an issue about this on Rust itself, because it may be possible to fix over there at some point (maybe in an edition): rust-lang/rust#86112

However, in the mean time, a clippy lint is probably more expedient.

What it does

Warns if the pattern provided to the matches! macro (or possibly to nay macro) contains a variable binding which is not also used within the pattern (such as a guard check).

Categories (optional)

  • Kind: clippy::correctness

What is the advantage of the recommended code over the original code

As explained in rust-lang/rust#86112:

A co-worker of mine tried this code:

if !matches!(binding_to_uuid, binding_to_other_uuid) {
    // ...
}

I expected to see this happen:

Compiler should say that this is an obvious error, because there is no way to use the binding_to_other_uuid binding because it is internal to the match statement (and just shadows the actual binding_to_other_uuid).

Instead, this happened:

The matches! macro here expands to something that is never useful:

if !match binding_to_uuid {
    binding_to_other_uuid => true,
    _ => false,
} { ... }

Clippy suggests underscoring the binding_to_other_uuid because it's unused, but that's not super clear unless you are familiar with the macro expansion. Ideally providing variable bindings that are not used in a guard would be disallowed in matches! because they cannot otherwise be used.

Drawbacks

None. The binding is unusable within that scope if it is not used as part of a guard.

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsgood 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