avoid-bool-literals-in-conditional-expressions
Same as avoid_bool_literals_in_conditional_expressions in Dart lints
- Group
- Style
- Path
lint/style/avoid-bool-literals-in-conditional-expressions- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 25 of 512
Flags conditional expressions over bool that have a bool literal branch.
When both branches of c ? a : b are non-nullable bools and one is a
literal, the expression simplifies to a logical operator: c ? true : b is
c || b, c ? false : b is !c && b, c ? b : true is !c || b, and
c ? b : false is c && b.
Branches are checked in their own flow state, after condition promotions.
Reports once per conditional: the analyzer deduplicates upstream reports
when both branches are literals at the same diagnostic location.
final ok = ready ? true : retry; // flagged
final ok = ready || retry; // ok
Invalid
void f(bool a, bool b, int i) {
final x = a ? true : b;
final y = a ? b : false;
final z = a ? false : b;
final w = a ? b : true;
final v = i > 0 ? true : false;
final u = a ? i > 2 : false;
print([x, y, z, w, v, u]);
}
// Source-declared numeric getter contracts keep these promotion cases SDK-free.
class Number {
bool get isInfinite => false;
bool get isNaN => false;
}
bool infinite(dynamic number) =>
number is Number ? number.isInfinite : false;
bool notANumber(dynamic number) =>
number is Number ? number.isNaN : false;
bool booleanPromotion(Object? value) =>
value is bool ? value : false;
bool inversePromotion(Object? value) =>
value is! bool ? true : value;void f(bool a, bool b, int i) { final x = a ? true : b; final x = a ? true : b; final y = a ? b : false; final y = a ? b : false; final z = a ? false : b; final z = a ? false : b; final w = a ? b : true; final w = a ? b : true; final v = i > 0 ? true : false; final v = i > 0 ? true : false; final u = a ? i > 2 : false;bool infinite(dynamic number) => number is Number ? number.isInfinite : false;bool notANumber(dynamic number) => number is Number ? number.isNaN : false;bool booleanPromotion(Object? value) => value is bool ? value : false;bool inversePromotion(Object? value) => value is! bool ? true : value;Valid
void f(bool a, bool b, bool? maybe, int i, dynamic d) {
final x = a || b;
final y = a ? b : !b;
final z = a ? maybe : true;
final w = a ? true : maybe;
final v = a ? 1 : 2;
final u = a ? d : false;
final t = a ? 'yes' : 'no';
print([x, y, z, w, v, u, t]);
}
Object? partialPromotion(Object? value) => value is bool ? true : value;
Object? inversePartialPromotion(Object? value) => value is! bool ? value : false;Configure avoid-bool-literals-in-conditional-expressions
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"avoid-bool-literals-in-conditional-expressions": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-bool-literals-in-conditional-expressions: <reason>More style rules
- avoid-catches-without-on-clauses
avoid_catches_without_on_clausesAvoid
catchclauses without anontype. - avoid-catching-errors
avoid_catching_errorsDon't explicitly catch
Erroror types that implement it. - avoid-double-and-int-checks
avoid_double_and_int_checksrecommendedAvoid checking a value for
doubleand then forint. - avoid-equals-and-hash-code-on-mutable-classes
avoid_equals_and_hash_code_on_mutable_classesFlags
==andhashCodeoverrides in classes not marked@immutable. - avoid-escaping-inner-quotes
avoid_escaping_inner_quotesFlags a string literal that escapes its own quote character when switching the outer quotes would remove every escape.
- avoid-field-initializers-in-const-classes
avoid_field_initializers_in_const_classesFlags field initializers in classes with a
constconstructor. - avoid-final-parameters
avoid_final_parametersFlags parameters declared
final. - avoid-implementing-value-types
avoid_implementing_value_typesFlags
implementsof a class that overrides==.