Description
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 actualbinding_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 inmatches!
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.