public function getLintErrorForNode()

in src/Linters/UnusedParameterLinter.hack [20:78]


  public function getLintErrorForNode(
    IFunctionishDeclaration $functionish,
    ParameterDeclaration $node,
  ): ?ASTLintError {
    if ($node->getVisibility() !== null) {
      // Constructor parameter promotion
      return null;
    }

    $name_node = $node->getName();
    if (!$name_node is VariableToken) {
      return null;
    }

    $name = $name_node->getText();
    if (Str\starts_with($name, '$_')) {
      return null;
    }

    // If this is a parameter of a lambda function, we should be looking in the
    // lambda's body, not the enclosing function/method's body.
    $lambda =
      $functionish->getClosestAncestorOfDescendantOfType<LambdaExpression>(
        $node,
      );

    if ($lambda is nonnull) {
      $body = $lambda->getBody();
    } else if ($functionish is FunctionDeclaration) {
      $body = $functionish->getBody();
    } else if ($functionish is MethodishDeclaration) {
      $body = $functionish->getFunctionBody();
    } else {
      invariant_violation(
        "Couldn't find functionish for parameter declaration",
      );
    }

    if ($body === null || $body is SemicolonToken) {
      // Don't require `$_` for abstract or interface methods
      return null;
    }

    foreach ($body->traverse() as $var) {
      if (!$var is VariableToken) {
        continue;
      }
      if ($var->getText() === $name) {
        return null;
      }
    }

    return new ASTLintError(
      $this,
      'Parameter is unused',
      $node,
      () ==> $this->getFixedNode($node),
    );
  }