no-boolean-literal-compare
Same as no-boolean-literal-compare in Dart Code Metrics
- Group
- Complexity
- Path
lint/complexity/no-boolean-literal-compare- Default
- On (recommended)
- Origin
- Dart Code Metrics
- Rule
- 281 of 512
Don't compare boolean values to boolean literals.
Invalid
// Test cases for no-boolean-literal-compare.
// Only comparisons whose non-literal operand is PROVABLY boolean are flagged.
void knownBooleanComparisons(bool a, bool b, Object o) {
if (true == false) {}
if (!a == true) {}
if ((a && b) == true) {}
if ((a || b) != false) {}
if ((a == b) == true) {}
if ((o is String) == true) {}
}
// The type-resolution layer widens the check to locals/params whose inferred
// static type is a non-nullable `bool`, not just syntactically-boolean operands.
void resolvedNonNullableBooleans(bool flag) {
if (flag == true) {}
bool ready = flag;
if (ready != false) {}
final bool done = flag && ready;
if (done == false) {}
}
class Widget {
bool isEnabled = true;
}
bool checkStatus() => true;
void resolvedMembersAndReturns(Widget foo) {
if (foo.isEnabled == true) {}
if (checkStatus() == false) {}
}void knownBooleanComparisons(bool a, bool b, Object o) { if (true == false) {} if (true == false) {} if (!a == true) {} if (!a == true) {} if ((a && b) == true) {} if ((a && b) == true) {} if ((a || b) != false) {} if ((a || b) != false) {} if ((a == b) == true) {} if ((a == b) == true) {} if ((o is String) == true) {}void resolvedNonNullableBooleans(bool flag) { if (flag == true) {} bool ready = flag; if (ready != false) {} final bool done = flag && ready; if (done == false) {}void resolvedMembersAndReturns(Widget foo) { if (foo.isEnabled == true) {} if (foo.isEnabled == true) {} if (checkStatus() == false) {}Valid
// Good cases for no-boolean-literal-compare rule
// No violations expected
void testBooleanDirect() {
if (isValid) {}
if (!isActive) {}
if (hasError) {}
if (!isEmpty) {}
}
void testBooleanNegation() {
if (!isReady) {}
if (!isDone) {}
}
void testInWhile() {
while (isRunning) {}
while (!shouldContinue) {}
}
bool checkStatus() {
return isInitialized;
}
void assertConditions() {
assert(isValid);
assert(!isDisabled);
}
class Widget {
bool isEnabled = true;
void setState() {
if (isEnabled) {
activate();
}
}
bool canRender() => isEnabled;
}
void ternaryWithBoolean() {
final status = isActive ? "on" : "off";
final result = !isValid ? "error" : "ok";
}
void multipleComparisons() {
if (flag1 && !flag2) {}
}
void methodCall(bool condition) {
process(condition);
}
void complexLogic(bool a, bool b, bool c) {
if (a && (b || !c)) {
print('condition met');
}
}
bool canAccess(bool isAuthenticated, bool isAuthorized) {
return isAuthenticated && isAuthorized;
}
void toggleState(bool current) {
final newState = !current;
update(newState);
}
class FeatureFlag {
bool isEnabled = false;
void check() {
if (!isEnabled) {
enable();
}
}
}
// Resolved non-nullable members and returns can be compared directly.
void nullableIdiom(bool? maybe, Widget foo) {
if (maybe == true) {}
if (maybe != false) {}
}
// A `bool?` local/param resolves to a *nullable* bool, so the resolver leaves
// `== true` alone — it is the idiomatic null-safe form, not a redundant compare.
void resolvedNullableBoolean(bool? maybe) {
bool? nb = maybe;
if (nb == true) {}
if (maybe != false) {}
}Configure no-boolean-literal-compare
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"no-boolean-literal-compare": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/no-boolean-literal-compare: <reason>More complexity rules
- one-member-abstracts
one_member_abstractsFlags abstract classes whose only member is a single abstract method.
- prefer-conditional-assignment
prefer_conditional_assignmentrecommendedFlags
if (x == null) { x = y; }with no else branch. - prefer-conditional-expressions
prefer-conditional-expressionsFlags an
if/elsewhose branches each reduce to a single return or a single assignment to the same target. - prefer-expression-function-bodies
prefer_expression_function_bodiesFlags block function bodies that consist of a single
returnof a value. - prefer-extracting-callbacks
prefer-extracting-callbacksflutterFlags inline block-body callbacks passed to Widget constructors.
- prefer-for-elements-to-map-from-iterable
prefer_for_elements_to_map_fromIterablerecommendedFlags
Map.fromIterable(x, key: ..., value: ...). - prefer-if-elements-to-conditional-expressions
prefer_if_elements_to_conditional_expressionsPrefers collection
ifelements to conditional expressions in list and set literals. - prefer-if-null-operators
prefer_if_null_operatorsrecommendedFlags
x == null ? y : x(and the invertedx != null ? x : y).