Skip to content

Prefer if over match #3520

@sobolevn

Description

@sobolevn

Rule request

Thesis

We need to force users to prefer if over simple match statements.

Example:

match state:
    case EventType.REJECT:
        user = 'rejected'
    case _:
        user = 'active'

Can be rewritten as:

if state == EventType.REJECT:
   user = 'rejected'
else:
   user = 'active'

Reasoning

Simple if is better, less nested, and more performant then match.

When can we raise this violation?

  1. When there are just two cases in match
  2. When the first case is simple, matching a single value or several values EventType.REJECT | EventType.REJECT2 | EventType.REJECT3
  3. When the second case is _ (so it can be converted to else easily`)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions