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.addExpressionStatement((ExpressionStatement node) {
    final Expression expr = node.expression;

    // Explicit unawaited() - recommended by Dart and by this rule's correction message
    if (expr is MethodInvocation && expr.methodName.name == 'unawaited') {
      return;
    }

    // Check if this is a method invocation that returns a Future
    if (expr is MethodInvocation) {
      final DartType? returnType = expr.staticType;
      if (returnType != null && _staticTypeIsFuture(returnType)) {
        // Skip safe patterns: subscription.cancel() in dispose(),
        // or chains ending with .catchError()/.ignore()
        if (_isSafeFireAndForget(expr, node)) {
          return;
        }
        reporter.atNode(expr);
      }
    }

    // Also check function invocations
    if (expr is FunctionExpressionInvocation) {
      final DartType? returnType = expr.staticType;
      if (returnType != null && _staticTypeIsFuture(returnType)) {
        reporter.atNode(expr);
      }
    }
  });
}