avoid-constant-assert-conditions

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

Flags assertions whose Boolean condition is statically constant. Remove an always-passing assertion or fix an always-failing condition.

Invalid

example.dartdart
const enabled = true;
void example() {
  assert(enabled);
  assert(1 == 2, 'failure');
  assert(true);
}
class Example {
  Example() : assert(enabled);
  const Example.named() : assert(1 < 2);
  Example.other() : assert(false);
}

class Settings {
  static const enabled = true;
  const Settings() : assert(enabled);
}

void constAllocationAssertions() {
  assert(const <int>[] == const <int>[]);
}
Assertion always passes; remove it or assert a runtime condition.
2void example() {
3 assert(enabled);
Assertion always fails; fix the condition or replace it with an explicit failure.
3 assert(enabled);
4 assert(1 == 2, 'failure');
Assertion always passes; remove it or assert a runtime condition.
4 assert(1 == 2, 'failure');
5 assert(true);
Assertion always passes; remove it or assert a runtime condition.
7class Example {
8 Example() : assert(enabled);
Assertion always passes; remove it or assert a runtime condition.
8 Example() : assert(enabled);
9 const Example.named() : assert(1 < 2);
Assertion always fails; fix the condition or replace it with an explicit failure.
9 const Example.named() : assert(1 < 2);
10 Example.other() : assert(false);
Assertion always passes; remove it or assert a runtime condition.
14 static const enabled = true;
15 const Settings() : assert(enabled);
Assertion always passes; remove it or assert a runtime condition.
18void constAllocationAssertions() {
19 assert(const <int>[] == const <int>[]);

Valid

example.dartdart
const enabled = true;
void example(bool enabled) {
  assert(enabled);
  assert(!enabled, 'runtime');
}
class Example {
  const Example(int value) : assert(value > 0);
}

void runtimeAndPatterns(bool enabled, Object value, List<bool> flags) {
  assert(enabled == true);
  assert(enabled != false);
  for (final enabled in flags) {
    assert(enabled);
  }
  if (value case bool enabled) {
    assert(enabled);
  }
  const local = true;
  // Captured locals have no environment-dependency provenance.
  assert(local);
  assert(const bool.fromEnvironment('FLAG'));
}
const environment = bool.hasEnvironment('FLAG');
const derived = !environment;
void environmentAssert() {
  assert(derived);
}
class InitializerShadow {
  const InitializerShadow(bool enabled) : assert(enabled);
}

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

void shadowedMethod() {}
class MethodShadow {
  void shadowedMethod() {}
  void check() {
    assert(shadowedMethod == shadowedMethod);
  }
}

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

class FreshValue { const FreshValue(); }
void freshAllocationAssertions() {
  assert(<int>[] == <int>[]);
  assert(<int>{} != <int>{});
  assert(identical(<int, int>{}, <int, int>{}));
  assert(FreshValue() == FreshValue());
  assert(identical((<int>[],), (<int>[],)));
}
void nestedRecordAssertion() {
  assert((<int>[],) == (<int>[],));
}

Configure avoid-constant-assert-conditions

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

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

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

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