use-if-null-to-convert-nulls-to-bools
Same as use_if_null_to_convert_nulls_to_bools in Dart lints
- Group
- Style
- Path
lint/style/use-if-null-to-convert-nulls-to-bools- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 492 of 512
Prefers ?? to comparing a nullable bool against a boolean literal.
For a bool? value b, b == true is b ?? false and b != false is
b ?? true; the if-null forms state the null default directly. The rule
fires on == with true and != with false on the right when the left
operand's static type is proven to be bool?.
Operand types include promotions proven at the comparison's use site. Parenthesized boolean literals do not match the official rule.
Invalid
bool? maybe() => null;
class Flags {
bool? enabled;
}
void check(bool? flag, Flags flags) {
if (flag == true) {}
if (flag != false) {}
print(maybe() == true);
print(flags.enabled != false);
final bool? local = flag;
print(local == true);
}
Future<bool?> answer() async => null;
Future<void> awaited() async {
print((await answer()) == true);
final result = await answer();
print(result != false);
}void check(bool? flag, Flags flags) { if (flag == true) {} if (flag == true) {} if (flag != false) {} if (flag != false) {} print(maybe() == true); print(maybe() == true); print(flags.enabled != false); final bool? local = flag; print(local == true);Future<void> awaited() async { print((await answer()) == true); final result = await answer(); print(result != false);Valid
void check(bool? flag, bool strict, Object? other) {
if (flag ?? false) {}
if (flag ?? true) {}
print(flag == false);
print(flag != true);
print(strict == true);
print(true == flag);
print(other == true);
print(flag == null);
}
void promoted(bool? early) {
if (early == null) return;
if (early == true) print(1);
}Configure use-if-null-to-convert-nulls-to-bools
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"use-if-null-to-convert-nulls-to-bools": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/use-if-null-to-convert-nulls-to-bools: <reason>More style rules
- use-late-for-private-fields-and-variables
use_late_for_private_fields_and_variablesFlags a private nullable field or top-level variable that is only ever assigned non-null values and read through
!, so it could belatewith a non-nullable type. - use-named-constants
use_named_constantsrecommendedPrefers a class's predefined static constant to constructing an equal constant.
- use-raw-strings
use_raw_stringsFlags string literals whose only escapes are
\\and\$, which a raw string would express without any backslash. - use-setters-to-change-properties
use_setters_to_change_propertiesFlags one-parameter
voidmethods that only assign that parameter to a field. - use-spacer-as-expanded-child
use_spacer_as_expanded_childrecommendedflutterFlags an
Expandedwrapping an emptySizedBoxorContainerthat should be aSpacer. - use-string-in-part-of-directives
use_string_in_part_of_directivesrecommendedFlags a
part ofdirective that names its library instead of using a string URI. - use-super-parameters
use_super_parametersrecommendedPrefer super parameters when a constructor only forwards parameters to its superclass.
- use-test-throws-matchers
use_test_throws_matchersrecommendedPrefers
throwsAmatchers overtry { ...; fail(...); } catch (...) {}.