blob: b97918bab4f2bb8f42ec08c97f15fe52bd670b7f [file] [log] [blame]
// 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:**
```dart
if (nullableBool == true) {
}
if (nullableBool != false) {
}
```
**GOOD:**
```dart
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;
var 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);
}