Class AvoidConstantAsFirstOperandInConditionCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.github.sevntu.checkstyle.checks.coding.AvoidConstantAsFirstOperandInConditionCheck
-
- All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable
,com.puppycrawl.tools.checkstyle.api.Contextualizable
public class AvoidConstantAsFirstOperandInConditionCheck extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
Checks that constants do not appear in the first operand of any condition.If comparing values, C(C++) developers prefer to put the constant first in the equality check, to prevent situations of assignment rather than equality checking. It is easy to write "=" instead of "==", and no compile error will be produced but condition will work in a different way then intended. However, in Java it is impossible to use assignment inside the
if
condition, so that habit becomes unnecessary and does damage to the readability of the code.This check was extended to include all equality checks like ">", ">=", "<", "<=" for users who prefer constants always be on the right-hand side for any condition.
Example:
if (null == variable)
rather thanif (variable == null)
because if you forget one (typo mistake) of the equal sign, you end up withif (variable = null)
which assigns null to variable and IF always evaluate to true.- Since:
- 1.9.0
- Author:
- Sergey Burtsev
-
-
Constructor Summary
Constructors Constructor Description AvoidConstantAsFirstOperandInConditionCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
int[]
getDefaultTokens()
int[]
getRequiredTokens()
void
setTargetConstantTypes(String... targets)
Set target constant types.void
visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST detailAST)
-
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, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
-
-
-
Field Detail
-
MSG_KEY
public static final String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
Method Detail
-
setTargetConstantTypes
public void setTargetConstantTypes(String... targets)
Set target constant types.- Parameters:
targets
- target constant types
-
getDefaultTokens
public int[] getDefaultTokens()
- Specified by:
getDefaultTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST detailAST)
- Overrides:
visitToken
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
-