Skip to content

Add MA0194 to merge repeated is-pattern checks#1107

Merged
meziantou merged 5 commits intomainfrom
meziantou/merge-is-patterns
Apr 29, 2026
Merged

Add MA0194 to merge repeated is-pattern checks#1107
meziantou merged 5 commits intomainfrom
meziantou/merge-is-patterns

Conversation

@meziantou
Copy link
Copy Markdown
Owner

Why

Pattern checks like value is A || value is B are harder to read and maintain than a single combined pattern. This change adds a dedicated rule to encourage the more idiomatic C# pattern-combinator form when both checks target the same value.

What changed

  • Added new rule id MA0194 (MergeIsPatternChecks) in RuleIdentifiers.
  • Added MergeIsPatternChecksAnalyzer to detect mergeable logical chains of is checks on the same expression.
  • Added MergeIsPatternChecksFixer to rewrite mergeable chains into combined or/and/not patterns while preserving precedence.
  • Added rule tests in MergeIsPatternChecksAnalyzerTests covering:
    • || merges
    • && merges including not
    • parenthesization behavior
    • no diagnostic when different variables are checked
  • Added docs/Rules/MA0194.md and regenerated generated documentation/config files (README.md, docs/README.md, and pack editorconfig files).

Notes for reviewers

  • For && chains of only positive constant patterns (for example value is A && value is B), the rule still reports, but the code fix intentionally does not merge that specific case because the direct and rewrite is rejected by the compiler (CS8518).

meziantou and others added 5 commits April 28, 2026 10:57
Introduce MA0194 with analyzer and fixer to merge repeated is-pattern checks on the same expression into combined patterns where valid. Add focused tests and regenerate rule documentation and generated rule tables/configuration.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Set MA0194 to be enabled by default at suggestion severity and add a rule metadata test. Regenerate documentation and default editorconfig outputs to reflect the new default.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@meziantou meziantou merged commit 1ce2e1f into main Apr 29, 2026
13 checks passed
@meziantou meziantou deleted the meziantou/merge-is-patterns branch April 29, 2026 00:04
This was referenced Apr 29, 2026
This was referenced May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant