Suggest using match
or matches!
instead of ==
for composite data structures known at compile time (tuples, structs, arrays, ...)
#13791
Labels
A-lint
Area: New lints
What it does
This lint would suggest using
match
ormatches!
instead of the==
operator when comparing a variable with a composite data structure (such as tuples, structs, arrays) that are known at compile time.Advantage
match
ormatches!
avoids unnecessary static data and SIMD overhead that might be introduced by the==
operator.Evidence
You can see the performance difference using Godbolt by examining the assembly outputs (under
-C opt-level=2
) for both the==
operator and thematches!
macro. The following Rust code compares a slice with an array using both approaches:Assembly Output for
test_eq
(using==
):Assembly Output for
test_match
(usingmatches!
):Drawbacks
This lint may create unnecessary noise.
Example
Could be written as:
The text was updated successfully, but these errors were encountered: