avoid-constant-conditions

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

Flags statically constant binary branch, loop, collection, and expression conditions. Replace the condition with the reachable branch or remove the unreachable code. Expressions containing runtime allocations are conservatively left unchanged.

Invalid

example.dartdart
const enabled = true;
void example(int x) {
  if (enabled == true) {}
  if (3 > 2) {}
  while (1 == 2) {}
  do {} while (1 == 2);
  for (; enabled == true; ) { break; }
  final result = enabled == true ? 1 : x;
  final list = [if (enabled == true) 1];
  final map = {if (enabled == true) 1: 2};
  print([result, list, map]);
}

void guards(Object value) {
  if (value case int _ when enabled == true) {}
  switch (value) {
    case int _ when enabled == true: print(value);
  }
  final result = switch (value) {
    int _ when enabled == true => 1,
    _ => 0,
  };
  print(result);
}

enum Choice { first, second }
class Settings {
  static const enabled = true;
}
void enumAndStatic() {
  if (Choice.first == Choice.second) {}
  if (Settings.enabled == true) {}
}

const implicitConstCondition = <int>[] == <int>[] ? 1 : 2;
const implicitConstCollection = [if (<int>[] == <int>[]) 1];
class ConstValue { const ConstValue(); }
const implicitConstConstructor = ConstValue() == ConstValue() ? 1 : 2;
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
2void example(int x) {
3 if (enabled == true) {}
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
3 if (enabled == true) {}
4 if (3 > 2) {}
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
4 if (3 > 2) {}
5 while (1 == 2) {}
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
5 while (1 == 2) {}
6 do {} while (1 == 2);
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
6 do {} while (1 == 2);
7 for (; enabled == true; ) { break; }
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
7 for (; enabled == true; ) { break; }
8 final result = enabled == true ? 1 : x;
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
8 final result = enabled == true ? 1 : x;
9 final list = [if (enabled == true) 1];
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
9 final list = [if (enabled == true) 1];
10 final map = {if (enabled == true) 1: 2};
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
14void guards(Object value) {
15 if (value case int _ when enabled == true) {}
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
16 switch (value) {
17 case int _ when enabled == true: print(value);
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
19 final result = switch (value) {
20 int _ when enabled == true => 1,
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
30void enumAndStatic() {
31 if (Choice.first == Choice.second) {}
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
31 if (Choice.first == Choice.second) {}
32 if (Settings.enabled == true) {}
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
34
35const implicitConstCondition = <int>[] == <int>[] ? 1 : 2;
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
35const implicitConstCondition = <int>[] == <int>[] ? 1 : 2;
36const implicitConstCollection = [if (<int>[] == <int>[]) 1];
Condition is statically constant; keep only the reachable branch or remove the unreachable code.
37class ConstValue { const ConstValue(); }
38const implicitConstConstructor = ConstValue() == ConstValue() ? 1 : 2;

Valid

example.dartdart
const enabled = true;
void example(bool enabled, int x) {
  if (enabled) {}
  while (x > 0) { x--; }
  do { x++; } while (enabled);
  for (; x < 10; x++) {}
  final result = enabled ? 1 : x;
  final list = [if (enabled) 1];
  final map = {if (enabled) 1: 2};
  print([result, list, map]);
}

const flag = true;
void shadowedPatterns(Object value) {
  if (value case bool flag when flag == true) {}
  switch (value) {
    case bool flag when flag == true: print(flag);
  }
  final values = [for (final flag in <bool>[true, false]) if (flag == true) 1];
  final maps = {for (final flag in <bool>[true, false]) if (flag == true) 1: 2};
  print([values, maps]);
}

void literalAndLocal() {
  if (true) {}
  while (true) { break; }
  const local = true;
  // Captured locals have no environment-dependency provenance.
  if (local == true) {}
  if (const bool.fromEnvironment('FLAG') == true) {}
}
const environment = bool.fromEnvironment('FLAG');
const derived = !environment;
void environmentCondition() {
  if (derived == true) {}
}

const shadowedGetter = true;
class GetterShadow {
  bool get shadowedGetter => DateTime.now().millisecondsSinceEpoch.isEven;
  void check() {
    if (shadowedGetter == true) {}
  }
}
class InheritedGetterShadow extends GetterShadow {
  void inheritedCheck() {
    if (shadowedGetter == true) {}
  }
}

void shadowedMethod() {}
class MethodShadow {
  void shadowedMethod() {}
  void check() {
    if (shadowedMethod == shadowedMethod) {}
  }
}

const extensionFlag = true;
extension on String {
  bool get extensionFlag => isEmpty;
  void checkUnnamedExtension() {
    if (extensionFlag == true) {}
  }
}
extension NamedFlag on List<int> {
  bool get extensionFlag => isEmpty;
  void checkNamedExtension() {
    if (extensionFlag == true) {}
  }
}

class FreshValue { const FreshValue(); }
void freshAllocationConditions() {
  if (<int>[] == <int>[]) {}
  if (<int>{} != <int>{}) {}
  if (identical(<int, int>{}, <int, int>{}) == true) {}
  if (FreshValue() == FreshValue()) {}
  if (identical((<int>[],), (<int>[],)) == true) {}
}
void nestedRecordCondition() {
  if ((<int>[],) == (<int>[],)) {}
}

Configure avoid-constant-conditions

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

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

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

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