Class UnnecessaryNullCheckWithInstanceOfCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.coding.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.
- Since:
- 10.25.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe error message key for reporting unnecessary null checks. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncheckChildForNullCheck(DetailAST currentChild, DetailAST instanceOfNode, DetailAST instanceOfIdent) Checks a child node for a redundant null check.private static booleancontainsVariableDereference(DetailAST node, String variableName) Checks if the given AST node contains a method call or field access on the specified variable.findDirectChildContaining(DetailAST parent, DetailAST target) Finds the direct child of parent that contains the target node.findRedundantNullCheck(DetailAST logicalAndNode, DetailAST instanceOfNode) Finds a redundant null check in a logical AND expression combined with an instanceof check.private static DetailASTTraverses up through LAND and LOR operators to find the top-level logical expression.findUnnecessaryNullCheck(DetailAST instanceOfNode) Checks for an unnecessary null check within a logical AND expression.int[]The configurable token set.int[]Returns the default token a check is interested in.int[]The tokens that this check must be registered for.private static booleanisAncestorOf(DetailAST node, DetailAST target) Checks if the given node is an ancestor of (or the same as) the target node.private static booleanisNotEqual(DetailAST node) Checks if the given AST node represents a!=(not equal) operator.private static booleanisNullCheckRedundant(DetailAST instanceOfIdent, DetailAST nullCheckNode) Determines if the null check is redundant with the instanceof check.private static booleanisNullLiteral(DetailAST node) Checks if the given AST node is a null literal.searchForNullCheck(DetailAST logicalAndNode, DetailAST instanceOfNode, DetailAST instanceOfIdent) Searches for a redundant null check in the children of a logical AND node.voidvisitToken(DetailAST instanceofNode) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_UNNECESSARY_NULLCHECK
The error message key for reporting unnecessary null checks.- See Also:
-
-
Constructor Details
-
UnnecessaryNullCheckWithInstanceOfCheck
-
-
Method Details
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe 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:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
instanceofNode- the token to process
-
findUnnecessaryNullCheck
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
-
findTopLevelLogicalExpression
Traverses up through LAND and LOR operators to find the top-level logical expression.- Parameters:
node- the starting node- Returns:
- the top-level logical expression node
-
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 expressioninstanceOfNode- the instanceof expression node- Returns:
- the AST node representing the redundant null check, or null if not found
-
searchForNullCheck
private static Optional<DetailAST> searchForNullCheck(DetailAST logicalAndNode, DetailAST instanceOfNode, DetailAST instanceOfIdent) Searches for a redundant null check in the children of a logical AND node.- Parameters:
logicalAndNode- the LAND node to searchinstanceOfNode- the instanceof expression nodeinstanceOfIdent- the identifier from the instanceof expression- Returns:
- the null check node if found, null otherwise
-
checkChildForNullCheck
private static Optional<DetailAST> checkChildForNullCheck(DetailAST currentChild, DetailAST instanceOfNode, DetailAST instanceOfIdent) Checks a child node for a redundant null check.- Parameters:
currentChild- the child node to checkinstanceOfNode- the instanceof expression nodeinstanceOfIdent- the identifier from the instanceof expression- Returns:
- the null check node if found, otherwise empty
-
findDirectChildContaining
Finds the direct child of parent that contains the target node.- Parameters:
parent- the parent nodetarget- the target node to find- Returns:
- the direct child containing target, or null if not found
-
isAncestorOf
Checks if the given node is an ancestor of (or the same as) the target node.- Parameters:
node- the potential ancestor nodetarget- the target node to check- Returns:
- true if node is an ancestor of target, false otherwise
-
containsVariableDereference
Checks if the given AST node contains a method call or field access on the specified variable.- Parameters:
node- the AST node to checkvariableName- the name of the variable- Returns:
- true if the variable is dereferenced, false otherwise
-
isNotEqual
Checks if the given AST node represents a!=(not equal) operator.- Parameters:
node- the AST node to check- Returns:
trueif the node is a not equal operator, otherwisefalse
-
isNullLiteral
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
Determines if the null check is redundant with the instanceof check.- Parameters:
instanceOfIdent- the identifier from the instanceof checknullCheckNode- the node representing the null check- Returns:
- true if the null check is unnecessary, false otherwise
-