Skip to main content

avoid_bool_literals_in_conditional_expressions

Details about the 'avoid_bool_literals_in_conditional_expressions' diagnostic produced by the Dart analyzer.

Conditional expressions with a 'bool' literal can be simplified.

Description

#

The analyzer produces this diagnostic when a conditional expression uses a boolean literal, and both branches are booleans. You can simplify these expressions using && or ||.

Examples

#

The following code produces this diagnostic because the then expression is the bool literal true:

dart
bool f(bool a, bool b) => a ? true : b;

The following code produces this diagnostic because the else expression is the bool literal false:

dart
bool f(bool a, bool b) => a ? b : false;

Common fixes

#

Replace the conditional expression with the equivalent logical expression. If the then expression is a boolean literal, replace it with one of the following:

dart
bool f(bool a, bool b) => a || b;  // replaces: a ? true : b
bool g(bool a, bool b) => !a && b; // replaces: a ? false : b

If the else expression is a boolean literal, replace it with one of the following:

dart
bool f(bool a, bool b) => !a || b; // replaces: a ? b : true
bool g(bool a, bool b) => a && b;  // replaces: a ? b : false