Skip to content

no_equal_switch_case

v1.0.0 Warning Control Flow

This rule flags two branches of a switch that produce identical bodies, where sharing the patterns would say the same thing once.

Repeating a body states the same outcome twice, and the two copies drift: one gets fixed and the other keeps the old behaviour, with nothing to show that they were ever meant to agree. Sharing the patterns (case a || b) says the outcome is deliberately the same and can only ever change in one place.

This rule is in the pedantic preset. Whether two independent enum branches that happen to agree today should be merged is a genuine judgement call — the strictest tier deliberately resolves that judgement in favour of one canonical branch.

Three shapes are deliberately not reported, because none of them can be merged into an || pattern:

  • A guarded case (case int() when x > 10) — each when belongs to its own pattern.
  • The catch-all (_ or default) — it has to stay last, and folding a specific case into it would change which values it covers.
  • An empty body — several empty cases in a row are how a fallthrough is written.
String label(int code) => switch (code) {
1 => 'ok',
2 => 'ok',
_ => 'error',
};
String label(int code) => switch (code) {
1 || 2 => 'ok',
_ => 'error',
};

This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:

many_lints.yaml
rules:
no_equal_switch_case:
enabled: true

To disable this rule:

many_lints.yaml
rules:
no_equal_switch_case: false

To keep the rule on but skip certain paths, use per-rule exclude.