avoid-duplicate-switch-case-conditions

Path
lint/correctness/avoid-duplicate-switch-case-conditions
Default
On (recommended)
Origin
Falcon
Rule
54 of 512
recommended

Flags repeated when guards on syntactically identical switch patterns.

Repeating a pattern and guard usually leaves the later arm unreachable. This rule compares guard and pattern tokens, preserving literal contents; Constant, literal, logical-or, and parenthesized patterns are left to no-duplicate-case-values. Different patterns may legitimately share a guard. Guards must use stable primitive variable reads and built-in comparisons; unknown getters, overloaded operators, and calls are not considered repeatable. The immutable String properties length, isEmpty, and isNotEmpty are supported on stable String variables.

Invalid

example.dartdart
int f(Object value, bool ready, int count) => switch (value) {
  int() when ready => 1,
  int() when ready => 2,
  String() when count > 1 => 3,
  String() when count>1 => 4,
  bool() when !ready => 5,
  bool() when !ready => 6,
  double() when count == 2 => 7,
  double() when count == 2 => 8,
  _ => 0,
};
void g(Object value, bool ready) {
  switch (value) {
    case int() when ready: break;
    case int() when ready: break;
    default: break;
  }
}

int stringGuards(Object value, String text) => switch (value) {
  int() when text.isEmpty => 1,
  int() when text.isEmpty => 2,
  String() when text.length > 2 => 3,
  String() when text.length > 2 => 4,
  bool() when text.isNotEmpty => 5,
  bool() when text.isNotEmpty => 6,
  _ => 0,
};
Remove or change this duplicate switch pattern and condition.
2 int() when ready => 1,
3 int() when ready => 2,
Remove or change this duplicate switch pattern and condition.
4 String() when count > 1 => 3,
5 String() when count>1 => 4,
Remove or change this duplicate switch pattern and condition.
6 bool() when !ready => 5,
7 bool() when !ready => 6,
Remove or change this duplicate switch pattern and condition.
8 double() when count == 2 => 7,
9 double() when count == 2 => 8,
Remove or change this duplicate switch pattern and condition.
14 case int() when ready: break;
15 case int() when ready: break;
Remove or change this duplicate switch pattern and condition.
21 int() when text.isEmpty => 1,
22 int() when text.isEmpty => 2,
Remove or change this duplicate switch pattern and condition.
23 String() when text.length > 2 => 3,
24 String() when text.length > 2 => 4,
Remove or change this duplicate switch pattern and condition.
25 bool() when text.isNotEmpty => 5,
26 bool() when text.isNotEmpty => 6,

Valid

example.dartdart
bool next() => true;
int f(Object value, bool ready, int count) => switch (value) {
  int() when ready => 1,
  String() when ready => 2,
  int() when !ready => 3,
  double() when count > 1 => 4,
  double() when count > 2 => 5,
  bool() when next() => 6,
  bool() when next() => 7,
  _ => 0,
};
int g(String value) => switch (value) {
  'a b' => 1,
  'ab' => 2,
  _ => 3,
};

bool get changing => next();
const code = 1;

int getterGuard(Object value) => switch (value) {
  int() when changing => 1,
  int() when changing => 2,
  _ => 0,
};

class Flags {
  bool get ready => next();
}

int customGetter(Object value, Flags flags) => switch (value) {
  int() when flags.ready => 1,
  int() when flags.ready => 2,
  _ => 0,
};

// Constant cases are owned by no-duplicate-case-values, even with guards.
int literalCases(int value, bool ready) => switch (value) {
  1 when ready => 1,
  1 when ready => 2,
  code when !ready => 3,
  code when !ready => 4,
  2 || 3 when ready => 5,
  2 || 3 when ready => 6,
  (4) when ready => 7,
  (4) when ready => 8,
  _ => 0,
};

int dynamicGuard(Object value, dynamic unknown) => switch (value) {
  int() when unknown == 1 => 1,
  int() when unknown == 1 => 2,
  _ => 0,
};

class StoredGuard {
  final bool ready = true;
  final String text = '';

  int guard(Object value) => switch (value) {
    int() when ready => 1,
    int() when ready => 2,
    String() when text.isEmpty => 3,
    String() when text.isEmpty => 4,
    _ => 0,
  };
}

class ChangingGuard extends StoredGuard {
  @override
  bool get ready => next();
  @override
  String get text => next() ? '' : 'changing';
}

bool globalReady = true;
int globalGuard(Object value) => switch (value) {
  int() when globalReady => 1,
  int() when globalReady => 2,
  _ => 0,
};

Configure avoid-duplicate-switch-case-conditions

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-duplicate-switch-case-conditions": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/avoid-duplicate-switch-case-conditions: <reason>