literal_ only_ boolean_ expressions
Boolean expression composed only with literals.
Details
#DON'T test for conditions composed only by literals, since the value can be inferred at compile time.
Conditional statements using a condition which cannot be anything but FALSE have
the effect of making blocks of code non-functional. If the condition cannot
evaluate to anything but
true
, the conditional statement is completely
redundant, and makes the code less readable.
It is quite likely that the code does not match the programmer's intent.
Either the condition should be removed or it should be updated so that it does
not always evaluate to
true
or
false
.
BAD:
void bad() {
if (true) {} // LINT
}
BAD:
void bad() {
if (true && 1 != 0) {} // LINT
}
BAD:
void bad() {
if (1 != 0 && true) {} // LINT
}
BAD:
void bad() {
if (1 < 0 && true) {} // LINT
}
BAD:
void bad() {
if (true && false) {} // LINT
}
BAD:
void bad() {
if (1 != 0) {} // LINT
}
BAD:
void bad() {
if (true && 1 != 0 || 3 < 4) {} // LINT
}
BAD:
void bad() {
if (1 != 0 || 3 < 4 && true) {} // LINT
}
NOTE:
that an exception is made for the common
while (true) { }
idiom,
which is often reasonably preferred to the equivalent
for (;;)
.
GOOD:
void good() {
while (true) {
// Do stuff.
}
}
Enable
#
To enable the
literal_only_boolean_expressions
rule, add
literal_only_boolean_expressions
under
linter > rules
in your
analysis_options.yaml
file:
linter:
rules:
- literal_only_boolean_expressions
If you're instead using the YAML map syntax to configure linter rules,
add
literal_only_boolean_expressions: true
under
linter > rules:
linter:
rules:
literal_only_boolean_expressions: true
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Report an issue.