Class UnnecessaryNullCheckWithInstanceOfCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks for redundant null checks with the instanceof operator.

The instanceof operator inherently returns false when the left operand is null, making explicit null checks redundant in boolean expressions with instanceof.

Parent is com.puppycrawl.tools.checkstyle.TreeWalker

Violation Message Keys:

  • unnecessary.nullcheck.with.instanceof
Since:
10.25.0
  • Field Details

  • Constructor Details

  • Method Details

    • getDefaultTokens

      public int[] getDefaultTokens()
      Description copied from class: AbstractCheck
      Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.
      Specified by:
      getDefaultTokens in class AbstractCheck
      Returns:
      the default tokens
      See Also:
    • getAcceptableTokens

      public int[] getAcceptableTokens()
      Description copied from class: AbstractCheck
      The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.
      Specified by:
      getAcceptableTokens in class AbstractCheck
      Returns:
      the token set this check is designed for.
      See Also:
    • getRequiredTokens

      public int[] getRequiredTokens()
      Description copied from class: AbstractCheck
      The tokens that this check must be registered for.
      Specified by:
      getRequiredTokens in class AbstractCheck
      Returns:
      the token set this must be registered for.
      See Also:
    • visitToken

      public void visitToken(DetailAST instanceofNode)
      Description copied from class: AbstractCheck
      Called to process a token.
      Overrides:
      visitToken in class AbstractCheck
      Parameters:
      instanceofNode - the token to process
    • findUnnecessaryNullCheck

      private static Optional<DetailAST> findUnnecessaryNullCheck(DetailAST instanceOfNode)
      Checks for an unnecessary null check within a logical AND expression.
      Parameters:
      instanceOfNode - the AST node representing the instanceof expression
      Returns:
      the identifier if the check is redundant, otherwise null
    • findRedundantNullCheck

      private static Optional<DetailAST> findRedundantNullCheck(DetailAST logicalAndNode, DetailAST instanceOfNode)
      Finds a redundant null check in a logical AND expression combined with an instanceof check.
      Parameters:
      logicalAndNode - the root node of the logical AND expression
      instanceOfNode - the instanceof expression node
      Returns:
      the AST node representing the redundant null check, or null if not found
    • containsVariableDereference

      private static boolean containsVariableDereference(DetailAST node, String variableName)
      Checks if the given AST node contains a method call or field access on the specified variable.
      Parameters:
      node - the AST node to check
      variableName - the name of the variable
      Returns:
      true if the variable is dereferenced, false otherwise
    • isNotEqual

      private static boolean isNotEqual(DetailAST node)
      Checks if the given AST node represents a != (not equal) operator.
      Parameters:
      node - the AST node to check
      Returns:
      true if the node is a not equal operator, otherwise false
    • isNullLiteral

      private static boolean isNullLiteral(DetailAST node)
      Checks if the given AST node is a null literal.
      Parameters:
      node - AST node to check
      Returns:
      true if the node is a null literal, false otherwise
    • isNullCheckRedundant

      private static boolean isNullCheckRedundant(DetailAST instanceOfIdent, DetailAST nullCheckNode)
      Determines if the null check is redundant with the instanceof check.
      Parameters:
      instanceOfIdent - the identifier from the instanceof check
      nullCheckNode - the node representing the null check
      Returns:
      true if the null check is unnecessary, false otherwise