avoid-constant-conditions
- Group
- Correctness
- 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
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;void example(int x) { if (enabled == true) {} if (enabled == true) {} if (3 > 2) {} if (3 > 2) {} while (1 == 2) {} while (1 == 2) {} do {} while (1 == 2); do {} while (1 == 2); for (; enabled == true; ) { break; } for (; enabled == true; ) { break; } final result = enabled == true ? 1 : x; final result = enabled == true ? 1 : x; final list = [if (enabled == true) 1]; final list = [if (enabled == true) 1]; final map = {if (enabled == true) 1: 2};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,void enumAndStatic() { if (Choice.first == Choice.second) {} if (Choice.first == Choice.second) {} if (Settings.enabled == true) {} const implicitConstCondition = <int>[] == <int>[] ? 1 : 2;const implicitConstCondition = <int>[] == <int>[] ? 1 : 2;const implicitConstCollection = [if (<int>[] == <int>[]) 1];class ConstValue { const ConstValue(); }const implicitConstConstructor = ConstValue() == ConstValue() ? 1 : 2;Valid
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.
{
"linter": {
"rules": {
"correctness": {
"avoid-constant-conditions": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-constant-conditions: <reason>More correctness rules
- 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.
- avoid-duplicate-factories
Flags factories in one type with the same implementation, ignoring factory names and alpha-renaming parameters by position.