no-duplicate-case-values

Same as no_duplicate_case_values in Pyramid Lint

Path
lint/suspicious/no-duplicate-case-values
Default
On (recommended)
Origin
Pyramid Lint
Rule
283 of 512
recommended

Flags semantically equal constant values within one switch.

Invalid

example.dartdart
// Bad: switch with duplicate case values
void processCode(int code) {
  switch (code) {
    case 1:
      print('first');
      break;
    case 1:
      print('duplicate');
      break;
    case 2:
      print('second');
      break;
  }
}

// Bad: duplicate string cases
void processStatus(String status) {
  switch (status) {
    case 'active':
      print('Active');
      break;
    case 'active':
      print('Also active');
      break;
    case 'inactive':
      print('Inactive');
      break;
  }
}

// Bad: duplicate boolean cases
void processBool(bool flag) {
  switch (flag) {
    case true:
      print('True branch');
      break;
    case true:
      print('Duplicate true');
      break;
    case false:
      print('False branch');
      break;
  }
}

// Bad: double duplicate cases
void processDouble(double value) {
  switch (value) {
    case 1.5:
      print('One point five');
      break;
    case 2.5:
      print('Two point five');
      break;
    case 1.5:
      print('Duplicate 1.5');
      break;
    case 2.5:
      print('Duplicate 2.5');
      break;
  }
}

const one = 1;
const alias = one;

int aliases(int value) => switch (value) {
  1 => 0,
  alias => 1,
  _ => 2,
};

int escaped(String value) => switch (value) {
  'a' => 0,
  '\x61' => 1,
  _ => 2,
};
Remove or change this duplicate case value in the switch statement.
6 break;
7 case 1:
Remove or change this duplicate case value in the switch statement.
21 break;
22 case 'active':
Remove or change this duplicate case value in the switch statement.
36 break;
37 case true:
Remove or change this duplicate case value in the switch statement.
54 break;
55 case 1.5:
Remove or change this duplicate case value in the switch statement.
57 break;
58 case 2.5:
Remove or change this duplicate case value in the switch statement.
68 1 => 0,
69 alias => 1,
Remove or change this duplicate case value in the switch statement.
74 'a' => 0,
75 '\x61' => 1,

Valid

example.dartdart
// Good: all case labels are unique
void processCode(int code) {
  switch (code) {
    case 1:
      print('first');
      break;
    case 2:
      print('second');
      break;
    case 3:
      print('third');
      break;
  }
}

// Good: unique string cases
void processStatus(String status) {
  switch (status) {
    case 'active':
      print('Active');
      break;
    case 'inactive':
      print('Inactive');
      break;
    case 'pending':
      print('Pending');
      break;
  }
}

// Good: boolean cases without duplication
void handleBool(bool value) {
  switch (value) {
    case true:
      print('True');
      break;
    case false:
      print('False');
      break;
  }
}

// Good: unique double values
void handleDouble(double value) {
  switch (value) {
    case 1.5:
      print('One point five');
      break;
    case 2.5:
      print('Two point five');
      break;
    case 3.5:
      print('Three point five');
      break;
  }
}

// Good: unique negative numbers
void handleNegative(int n) {
  switch (n) {
    case -1:
      print('Negative one');
      break;
    case -2:
      print('Negative two');
      break;
    case 0:
      print('Zero');
      break;
    case 1:
      print('One');
      break;
  }
}

Configure no-duplicate-case-values

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "no-duplicate-case-values": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/suspicious/no-duplicate-case-values: <reason>
  • no-empty-blockno-empty-block

    Flags empty blocks ({}).

  • no-equal-argumentsno-equal-arguments

    Flags an argument passed more than once in the same invocation.

  • no-equal-then-elseno-equal-then-elserecommended

    Flags an if/else or ternary whose two branches are identical.

  • no-magic-string

    Extract inline string literals into named variables or constants.

  • no-self-comparisonsno_self_comparisonsrecommended

    Flags proven self-comparisons of side-effect-free scalar values.

  • no-wildcard-variable-usesno_wildcard_variable_usesrecommended

    Flags references to a wildcard variable or parameter (a name made solely of underscores, such as _ or __).

  • overridden-fieldsoverridden_fieldsrecommended

    Flags instance fields that redeclare an inherited field.

  • prefer-unique-test-namestest

    Reports duplicate test descriptions within a group and constant names in loops.