Skip to content

matches! accepts unusable bindings in the pattern #86112

@Fishrock123

Description

@Fishrock123

Perhaps this is more a thing for clippy to detect, I'm not sure.

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.

Meta

rustc --version --verbose:

rustc 1.52.1 (9bc8c42bb 2021-05-09)
binary: rustc
commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
commit-date: 2021-05-09
host: x86_64-unknown-linux-gnu
release: 1.52.1
LLVM version: 12.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions