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 TypeMethodDescriptionprivate static booleancontainsVariableDereference(DetailAST node, String variableName) Checks if the given AST node contains a method call or field access on the specified variable.findRedundantNullCheck(DetailAST logicalAndNode, DetailAST instanceOfNode) Finds a redundant null check in a logical AND expression combined with an instanceof check.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 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.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
-
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
-
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
-