check method

  1. @override
void check(
  1. DcqRegistry registry
)

Implementation

@override
void check(
  DcqRegistry registry,
) {
  registry.addSpreadElement((node) {
    if (node.isNullAware) return;
    final expr = node.expression;
    if (expr is! BinaryExpression) return;
    if (expr.operator.lexeme != '??') return;
    final right = expr.rightOperand;
    if (right is ListLiteral && right.elements.isEmpty ||
        right is SetOrMapLiteral && right.elements.isEmpty) {
      reportAtNode(node);
    }
  });

  registry.addIfElement((node) {
    if (node.elseElement != null) return;

    final condition = node.expression;
    if (condition is! BinaryExpression) return;
    if (condition.operator.lexeme != '!=') return;

    final String? checkedName;
    if (condition.rightOperand is NullLiteral) {
      checkedName = condition.leftOperand.toSource();
    } else if (condition.leftOperand is NullLiteral) {
      checkedName = condition.rightOperand.toSource();
    } else {
      return;
    }

    final thenElement = node.thenElement;
    if (thenElement is! SpreadElement) return;

    if (thenElement.expression.toSource() == checkedName) {
      reportAtNode(node);
    }
  });
}