Skip to content

Commit

Permalink
Added exemption to the reportUnnecessaryComparison rule for simple …
Browse files Browse the repository at this point in the history
…wildcard patterns in a `case` clause. This addresses #4706. (#9582)
  • Loading branch information
erictraut authored Dec 13, 2024
1 parent f1170c0 commit 9041f88
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ The following settings allow more fine grained control over the **typeCheckingMo

- <a name="reportUnnecessaryCast"></a> **reportUnnecessaryCast** [boolean or string, optional]: Generate or suppress diagnostics for `cast` calls that are statically determined to be unnecessary. Such calls are sometimes indicative of a programming error. The default value for this setting is `"none"`.

- <a name="reportUnnecessaryComparison"></a> **reportUnnecessaryComparison** [boolean or string, optional]: Generate or suppress diagnostics for `==` or `!=` comparisons or other conditional expressions that are statically determined to always evaluate to False or True. Such comparisons are sometimes indicative of a programming error. The default value for this setting is `"none"`.
- <a name="reportUnnecessaryComparison"></a> **reportUnnecessaryComparison** [boolean or string, optional]: Generate or suppress diagnostics for `==` or `!=` comparisons or other conditional expressions that are statically determined to always evaluate to False or True. Such comparisons are sometimes indicative of a programming error. The default value for this setting is `"none"`. Also reports `case` clauses in a `match` statement that can be statically determined to never match (with exception of the `_` wildcard pattern, which is exempted).

- <a name="reportUnnecessaryContains"></a> **reportUnnecessaryContains** [boolean or string, optional]: Generate or suppress diagnostics for `in` operations that are statically determined to always evaluate to False or True. Such operations are sometimes indicative of a programming error. The default value for this setting is `"none"`.

Expand Down
10 changes: 10 additions & 0 deletions packages/pyright-internal/src/analyzer/patternMatching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2174,6 +2174,16 @@ export function getPatternSubtypeNarrowingCallback(
}

function reportUnnecessaryPattern(evaluator: TypeEvaluator, pattern: PatternAtomNode, subjectType: Type): void {
// If this is a simple wildcard pattern, exempt it from this diagnostic.
if (
pattern.nodeType === ParseNodeType.PatternAs &&
pattern.d.orPatterns.length === 1 &&
pattern.d.orPatterns[0].nodeType === ParseNodeType.PatternCapture &&
pattern.d.orPatterns[0].d.isWildcard
) {
return;
}

evaluator.addDiagnostic(
DiagnosticRule.reportUnnecessaryComparison,
LocMessage.patternNeverMatches().format({ type: evaluator.printType(subjectType) }),
Expand Down
11 changes: 11 additions & 0 deletions packages/pyright-internal/src/tests/samples/matchUnnecessary1.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,14 @@ def func4(vals: list[str]) -> TA1:
case _:
pass
return x


def func5(subj: int | str):
match subj:
case int() | str():
pass

# This should not generate a diagnostic becuase _ is exempted
# from the reportUnnecessaryComparison check.
case _:
pass

0 comments on commit 9041f88

Please sign in to comment.