literal-only-boolean-expressions
Same as literal_only_boolean_expressions in Dart lints
- Group
- Correctness
- Path
lint/correctness/literal-only-boolean-expressions- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 257 of 512
Flags conditions built only from literals, whose value never changes.
An if, while, do-while, or for condition (or a pattern when
guard) made solely of literals combined with operators always evaluates the
same way, so the branch is either dead or unconditional. while (true) is
allowed as the idiomatic infinite loop. Guard diagnostics include the when keyword.
if (1 > 2) {} // flagged
while (true && false) {} // flagged
while (true) {} // ok
if (x > 2) {} // ok
Invalid
void bad(int x) {
if (true) {}
if (1 > 2) {}
if (!false) {}
if ('a' == 'b' || null == null) {}
while (true && false) {}
do {} while (false);
for (var i = 0; 1 < 2; i++) {}
if (1 is int) {}
if (null ?? x > 1) {}
switch (x) {
case 1 when true:
print(x);
default:
}
final y = switch (x) {
1 when 2 > 1 => 'a',
_ => 'b',
};
print(y);
}
void constantInterpolation() {
const a = 20;
if ('$a' == '20') {}
if ('${1 + 2}' == '3') {}
const flag = true;
if ('${flag ? a : 0}' == '20') {}
}void bad(int x) { if (true) {} if (true) {} if (1 > 2) {} if (1 > 2) {} if (!false) {} if (!false) {} if ('a' == 'b' || null == null) {} if ('a' == 'b' || null == null) {} while (true && false) {} while (true && false) {} do {} while (false); do {} while (false); for (var i = 0; 1 < 2; i++) {} for (var i = 0; 1 < 2; i++) {} if (1 is int) {} if (1 is int) {} if (null ?? x > 1) {} switch (x) { case 1 when true: final y = switch (x) { 1 when 2 > 1 => 'a', const a = 20; if ('$a' == '20') {} if ('$a' == '20') {} if ('${1 + 2}' == '3') {} const flag = true; if ('${flag ? a : 0}' == '20') {}Valid
void good<T>(int x, bool flag, String s) {
if (x > 2) {}
if (flag) {}
if (flag && true) {}
while (true) {}
}
void more<T>(int x, String s) {
do {} while (x > 0);
for (var i = 0; i < 2; i++) {}
for (;;) {
break;
}
for (final e in [1, 2]) {
print(e);
}
if (1 is T) {}
if ('$x' == '1') {}
if (x case 1) {}
switch (x) {
case 1 when x > 0:
print(x);
default:
}
final list = [if (x > 1) 1];
print(list);
}
void nonConstantInterpolation() {
final a = 20;
if ('$a' == '20') {}
const outer = 20;
void shadowed(int outer) {
if ('$outer' == '20') {}
}
shadowed(outer);
}Configure literal-only-boolean-expressions
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"literal-only-boolean-expressions": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/literal-only-boolean-expressions: <reason>More correctness rules
- match-getter-setter-field-names
Detect likely copy-paste errors in trivial accessors when a different field matching the accessor name is declared.
- missing-test-assertionrecommendedtest
Reports inline test callbacks that contain no recognized assertion calls.
- no-adjacent-strings-in-list
no_adjacent_strings_in_listrecommendedFlags adjacent string literals used as an element of a list or set literal.
- no-import-cyclescross-file
Report mutually dependent components of imported or exported library files.
- no-logic-in-create-state
no_logic_in_create_staterecommendedflutterRequire
StatefulWidget.createStateto return only a fresh zero-argument FlutterStateconstruction. - no-self-assignments
no_self_assignmentsrecommendedFlags assignments of a variable to itself, such as
x = x;. - null-check-on-nullable-type-parameter
null_check_on_nullable_type_parameterrecommendedFlags a null assertion
x!on a nullable type parameterT?whose expected type isTorT?for a potentially nullableT, and null-assert patterns whose matched value is a nullable type parameter. - null-closures
null_closuresrecommendedReject literal
nullin the closure slots of curated SDK APIs.