Skip to content

Prefer if over match with single-case #3528

@ZhdanovAM72

Description

@ZhdanovAM72

Rule request

Thesis

We should support simplifying match statements with only one case and no wildcard:

Example:

match x:
    case 1:
        do_something()

Can be rewritten as:

if x == 1:
    do_something()

Reasoning

Even without an else branch, a single-case match is unnecessarily complex compared to a simple if. The intent is clear, and using if reduces nesting and cognitive load.

This is especially common in validation or dispatch logic where only one condition matters.

When should this be allowed?

Only when:

  1. There is exactly one case
  2. The pattern is simple (literal, constant, enum, etc.)
  3. No guard (if ...) is used

Originally posted by @sobolevn in #3526 (review)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions