no-literal-bool-comparisons
Same as no_literal_bool_comparisons in Dart lints
- Group
- Style
- Path
lint/style/no-literal-bool-comparisons- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 290 of 512
Flags boolean literals compared or combined with a non-nullable bool.
x == true is just x, x != true is !x, x && true is x, and so on
for ||, &, |, and ^. The literal is only redundant when the other
operand is provably a non-nullable bool; a bool? compared to true is a
meaningful null check and is left alone.
Parenthesized literals are separate expressions and are not reported.
if (done == true) {} // flagged
if (done) {} // ok
Invalid
void f(bool a, bool b, bool? maybe) {
if (a == true) {}
if (false != a) {}
if (a && true) {}
if (false || b) {}
final c = a & true;
final d = b ^ false;
final e = (a == b) == true;
print([c, d, e, maybe]);
}
bool ready() => true;
void g(Object o) {
if (ready() == false) {}
if ((o is String) != true) {}
}void f(bool a, bool b, bool? maybe) { if (a == true) {} if (a == true) {} if (false != a) {} if (false != a) {} if (a && true) {} if (a && true) {} if (false || b) {} if (false || b) {} final c = a & true; final c = a & true; final d = b ^ false; final d = b ^ false; final e = (a == b) == true;void g(Object o) { if (ready() == false) {} if (ready() == false) {} if ((o is String) != true) {}Valid
void f(bool a, bool? maybe, dynamic d, Object o, int i) {
if (a) {}
if (!a) {}
if (maybe == true) {}
if (maybe != false) {}
if (d == true) {}
if (o == true) {}
if (i == 1) {}
if (a == maybe) {}
final x = maybe ?? false;
print(x);
}Configure no-literal-bool-comparisons
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"no-literal-bool-comparisons": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/no-literal-bool-comparisons: <reason>More style rules
- no-magic-number
no-magic-numberFlags unnamed numeric literals (magic numbers) used inline in expressions.
- no-object-declaration
no-object-declarationFlags class members declared with the type
Object(orObject?). - no-runtime-type-to-string
no_runtimeType_toStringAvoid calling
toString()onruntimeType. - non-constant-identifier-names
non_constant_identifier_namesrecommendedFlags non-constant identifiers that are not written in lowerCamelCase.
- omit-local-variable-types
omit_local_variable_typesFlags local variable type annotations that repeat the initializer's type.
- only-throw-errors
only_throw_errorsrecommendedOnly throw instances of classes that extend
ExceptionorError. - package-names
package_namesrecommendedcross-fileRequire package names to be valid
lower_case_with_underscoresidentifiers. - parameter-assignments
parameter_assignmentsDon't reassign parameters of functions, methods, or constructors.