Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ linter:
- unsafe_html
- use_full_hex_values_for_flutter_colors
- use_function_type_syntax_for_parameters
- use_if_null_to_convert_nulls_to_bools
- use_is_even_rather_than_modulo
- use_key_in_widget_constructors
- use_late_for_private_fields_and_variables
Expand Down
2 changes: 2 additions & 0 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ import 'rules/unrelated_type_equality_checks.dart';
import 'rules/unsafe_html.dart';
import 'rules/use_full_hex_values_for_flutter_colors.dart';
import 'rules/use_function_type_syntax_for_parameters.dart';
import 'rules/use_if_null_to_convert_nulls_to_bools.dart';
import 'rules/use_is_even_rather_than_modulo.dart';
import 'rules/use_key_in_widget_constructors.dart';
import 'rules/use_late_for_private_fields_and_variables.dart';
Expand Down Expand Up @@ -364,6 +365,7 @@ void registerLintRules() {
..register(UnsafeHtml())
..register(UseFullHexValuesForFlutterColors())
..register(UseFunctionTypeSyntaxForParameters())
..register(UseIfNullToConvertNullsToBools())
..register(UseIsEvenRatherThanModuloCheck())
..register(UseKeyInWidgetConstructors())
..register(UseLateForPrivateFieldsAndVariables())
Expand Down
84 changes: 84 additions & 0 deletions lib/src/rules/use_if_null_to_convert_nulls_to_bools.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/type.dart';

import '../analyzer.dart';

const _desc = r'Use if-null operators to convert nulls to bools.';

const _details = r'''

Use if-null operators to convert nulls to bools.

**BAD:**
```
if (nullableBool == true) {
}
if (nullableBool != false) {
}
```

**GOOD:**
```
if (nullableBool ?? false) {
}
if (nullableBool ?? true) {
}
```

''';

class UseIfNullToConvertNullsToBools extends LintRule implements NodeLintRule {
UseIfNullToConvertNullsToBools()
: super(
name: 'use_if_null_to_convert_nulls_to_bools',
description: _desc,
details: _details,
group: Group.style,
);

@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
if (!context.isEnabled(Feature.non_nullable)) return;

final visitor = _Visitor(this, context);
registry.addBinaryExpression(this, visitor);
}
}

class _Visitor extends SimpleAstVisitor<void> {
final LintRule rule;
final LinterContext context;

_Visitor(this.rule, this.context);

@override
void visitBinaryExpression(BinaryExpression node) {
var type = node.leftOperand.staticType;
var right = node.rightOperand;
if (node.operator.type == TokenType.EQ_EQ &&
isNullableBool(type) &&
right is BooleanLiteral &&
right.value) {
rule.reportLint(node);
}
if (node.operator.type == TokenType.BANG_EQ &&
isNullableBool(type) &&
right is BooleanLiteral &&
!right.value) {
rule.reportLint(node);
}
}

bool isNullableBool(DartType? type) =>
type != null &&
type.isDartCoreBool &&
context.typeSystem.isNullable(type);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// test w/ `pub run test -N use_if_null_to_convert_nulls_to_bools`

m() {
bool? e;
bool r;
r = e == true; // LINT
r = e == false; // OK
r = e ?? false; // OK
r = e != false; // LINT
r = e != true; // OK
r = e ?? true; // OK
}