avoid-constant-switches

Path
lint/correctness/avoid-constant-switches
Default
Off, opt in
Origin
Falcon
Rule
40 of 512

Flags switch statements and expressions with a statically constant subject. Replace the switch with the matching branch.

Invalid

example.dartdart
const selected = 2;
void example() {
  switch (selected) {
    case 2: print('two');
    default: print('other');
  }
  final value = switch ('yes') {
    'yes' => 1,
    _ => 0,
  };
  switch (1 + 2) {
    case 3: print(value);
  }
}

enum Choice { first, second }
class Settings {
  static const selected = 2;
}
void enumAndStatic() {
  switch (Choice.first) {
    case Choice.first: break;
    case Choice.second: break;
  }
  switch (Settings.selected) {
    case 2: break;
    default: break;
  }
  final value = switch (Choice.second) {
    Choice.first => 1,
    Choice.second => 2,
  };
  print(value);
}
Switch subject is statically constant; replace the switch with the matching branch.
2void example() {
3 switch (selected) {
Switch subject is statically constant; replace the switch with the matching branch.
6 }
7 final value = switch ('yes') {
Switch subject is statically constant; replace the switch with the matching branch.
10 };
11 switch (1 + 2) {
Switch subject is statically constant; replace the switch with the matching branch.
20void enumAndStatic() {
21 switch (Choice.first) {
Switch subject is statically constant; replace the switch with the matching branch.
24 }
25 switch (Settings.selected) {
Switch subject is statically constant; replace the switch with the matching branch.
28 }
29 final value = switch (Choice.second) {

Valid

example.dartdart
const selected = 2;
void example(int selected) {
  switch (selected) {
    case 2: print('two');
    default: print('other');
  }
  final value = switch (selected) {
    2 => 'two',
    _ => 'other',
  };
  print(value);
}

void runtimeAndPatterns(Object value, List<int> values) {
  for (final selected in values) {
    switch (selected) {
      case 2: break;
      default: break;
    }
  }
  if (value case int selected) {
    final result = switch (selected) { 2 => true, _ => false };
    print(result);
  }
  final result = [
    for (final selected in values)
      switch (selected) { 2 => true, _ => false },
  ];
  const local = 2;
  // Captured locals have no environment-dependency provenance.
  switch (local) {
    case 2: break;
    default: break;
  }
  switch (const int.fromEnvironment('NUMBER')) {
    case 2: break;
    default: break;
  }
  print(result);
}
const environment = String.fromEnvironment('MODE');
const derived = environment == 'debug';
void environmentSwitch() {
  switch (derived) {
    case true: break;
    case false: break;
  }
}

const shadowedGetter = 1;
class GetterShadow {
  int get shadowedGetter => DateTime.now().millisecondsSinceEpoch;
  void check() {
    switch (shadowedGetter) {
      case 1: break;
      default: break;
    }
  }
}
class InheritedGetterShadow extends GetterShadow {
  void inheritedCheck() {
    switch (shadowedGetter) {
      case 1: break;
      default: break;
    }
  }
}

void shadowedMethod() {}
class MethodShadow {
  void shadowedMethod() {}
  void check() {
    switch (shadowedMethod) {
      default: break;
    }
  }
}

const extensionFlag = true;
extension on String {
  bool get extensionFlag => isEmpty;
  void checkUnnamedExtension() {
    switch (extensionFlag) {
      case true: break;
      case false: break;
    }
  }
}
extension NamedFlag on List<int> {
  bool get extensionFlag => isEmpty;
  void checkNamedExtension() {
    switch (extensionFlag) {
      case true: break;
      case false: break;
    }
  }
}

class FreshValue { const FreshValue(); }
void freshAllocationSwitches() {
  switch (<int>[] == <int>[]) { case true: break; default: break; }
  switch (<int>{} != <int>{}) { case true: break; default: break; }
  switch (identical(<int, int>{}, <int, int>{})) { case true: break; default: break; }
  switch (FreshValue() == FreshValue()) { case true: break; default: break; }
  final result = switch (identical((<int>[],), (<int>[],))) { true => 1, _ => 2 };
  print(result);
}
void nestedRecordSwitch() {
  switch ((<int>[],) == (<int>[],)) { case true: break; default: break; }
}

Configure avoid-constant-switches

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-constant-switches": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/avoid-constant-switches: <reason>