Add matches_with_unrelated_if lint#16870
Open
Souradip121 wants to merge 3 commits intorust-lang:masterfrom
Open
Add matches_with_unrelated_if lint#16870Souradip121 wants to merge 3 commits intorust-lang:masterfrom
matches_with_unrelated_if lint#16870Souradip121 wants to merge 3 commits intorust-lang:masterfrom
Conversation
Collaborator
|
r? @Jarcho rustbot has assigned @Jarcho. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
Warns when matches!(expr, Pattern if guard) is used and the guard does not reference any variable introduced by the pattern. The guard is unrelated to the match and belongs outside as matches!(expr, Pattern) && guard.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #16719
What this PR does
Adds a new
stylelintmatches_with_unrelated_ifthat warns whenmatches!(expr, Pattern if guard)is used and the guard does notreference any variable introduced by
Pattern.Motivation
The
ifguard insidematches!implies a relationship between thepattern bindings and the condition. When the guard is independent of the
pattern, the grouping is misleading. Moving it out makes the intent clearer:
This also helps catch cases where the user intended to use a pattern
binding in the guard but made an oversight (e.g. typed
f.is_bar()instead of
x.is_bar()when the pattern wasSome(x)).Implementation
clippy_lints/src/matches/matches_with_unrelated_if.rsdeclared_lints.rsandmatches/mod.rsstyle, machine-applicable suggestionTests
Added
tests/ui/matches_with_unrelated_if.rscovering:changelog: new lint [
matches_with_unrelated_if]