runWithReporter method

  1. @override
void runWithReporter(
  1. SaropaDiagnosticReporter reporter,
  2. SaropaContext context
)
override

Override this method to implement your lint rule.

Use context to register callbacks for AST node types:

context.addMethodInvocation((node) {
  if (condition) {
    reporter.atNode(node);
  }
});

Implementation

@override
void runWithReporter(
  SaropaDiagnosticReporter reporter,
  SaropaContext context,
) {
  context.addBinaryExpression((BinaryExpression node) {
    // Only check == and != operators
    final operatorType = node.operator.type;
    if (operatorType != TokenType.EQ_EQ &&
        operatorType != TokenType.BANG_EQ) {
      return;
    }

    final left = node.leftOperand;
    final right = node.rightOperand;

    // Check if either operand is a boolean literal
    final leftIsBoolLiteral = left is BooleanLiteral;
    final rightIsBoolLiteral = right is BooleanLiteral;

    if (!leftIsBoolLiteral && !rightIsBoolLiteral) {
      return;
    }

    // Check if the other operand is a boolean type (to avoid false positives
    // on nullable booleans where == true is intentional)
    final Expression otherOperand = leftIsBoolLiteral ? right : left;
    final otherType = otherOperand.staticType;

    // Only flag if the other operand is non-nullable bool
    if (otherType != null && otherType.isDartCoreBool) {
      // Check nullability - allow == true/false for nullable bools
      if (otherType.nullabilitySuffix == NullabilitySuffix.none) {
        reporter.atNode(node);
      }
    }
  });
}