check method

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

Implementation

@override
void check(
  DcqRegistry registry,
) {
  registry.addMethodDeclaration((node) {
    if (!node.isGetter) return;
    if (node.isAbstract) return;

    final body = node.body;
    SimpleIdentifier? fieldRef;
    if (body is ExpressionFunctionBody) {
      final expression = body.expression;
      if (expression is SimpleIdentifier) fieldRef = expression;
    } else if (body is BlockFunctionBody) {
      final statements = body.block.statements;
      if (statements.length == 1) {
        final statement = statements.first;
        if (statement is ReturnStatement) {
          final expression = statement.expression;
          if (expression is SimpleIdentifier) fieldRef = expression;
        }
      }
    }
    if (fieldRef == null) return;

    if (configBool(ruleConfig, 'ignore-upcast') ?? true) {
      final getterType = node.returnType?.type;
      final fieldType = fieldRef.staticType;
      if (getterType != null &&
          fieldType != null &&
          getterType != fieldType) {
        return;
      }
    }

    reportAtToken(node.name);
  });
}