Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added exemption to the reportUnnecessaryComparison rule for simple … #9582

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading