avoid-constant-assert-conditions
- Group
- Correctness
- 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
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>[]);
}void example() { assert(enabled); assert(enabled); assert(1 == 2, 'failure'); assert(1 == 2, 'failure'); assert(true);class Example { Example() : assert(enabled); Example() : assert(enabled); const Example.named() : assert(1 < 2); const Example.named() : assert(1 < 2); Example.other() : assert(false); static const enabled = true; const Settings() : assert(enabled);void constAllocationAssertions() { assert(const <int>[] == const <int>[]);Valid
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.
{
"linter": {
"rules": {
"correctness": {
"avoid-constant-assert-conditions": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-constant-assert-conditions: <reason>More correctness rules
- avoid-constant-conditions
Flags statically constant binary branch, loop, collection, and expression conditions.
- avoid-constant-switches
Flags switch statements and expressions with a statically constant subject.
- avoid-contradictory-expressionsrecommended
Flags pairs of conditions that are always false with
&&or true with||. - avoid-disposing-late-fieldsflutter
Avoid releasing late State fields that may be unset or initialized only by cleanup.
- avoid-duplicate-bloc-event-handlersrecommendedbloc
Flags later registrations of the same event type in a Bloc constructor.
- avoid-duplicate-cascades
Flags repeated field or index assignment targets within one cascade.
- avoid-duplicate-collection-elements
Flags repeated scalar literals and stable local reads in list and set literals.
- avoid-duplicate-exports
Flags export directives whose visible names are already covered by another export.