Issue #572 BooleanExpressionComplexity misidentifies integer expression as boolean expression

This commit is contained in:
ychulovskyy 2015-02-25 21:37:24 +01:00 committed by Roman Ivanov
parent f08c38b631
commit 03914ce3a0
4 changed files with 67 additions and 4 deletions

View File

@ -27,6 +27,9 @@ import com.puppycrawl.tools.checkstyle.checks.CheckUtils;
/**
* Restricts nested boolean operators (&&, ||, &, | and ^) to
* a specified depth (default = 3).
* Note: &, | and ^ are not checked if they are part of constructor or
* method call because they can be applied to non boolean variables and
* Checkstyle does not know types of methods from different classes.
*
* @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
* @author o_sukhodolsky
@ -126,14 +129,18 @@ public final class BooleanExpressionComplexityCheck extends Check
visitExpr();
break;
case TokenTypes.BOR:
if (!isPipeOperator(ast)) {
if (!isPipeOperator(ast) && !isPassedInParameter(ast)) {
context.visitBooleanOperator();
}
break;
case TokenTypes.BAND:
case TokenTypes.BXOR:
if (!isPassedInParameter(ast)) {
context.visitBooleanOperator();
}
break;
case TokenTypes.LAND:
case TokenTypes.BAND:
case TokenTypes.LOR:
case TokenTypes.BXOR:
context.visitBooleanOperator();
break;
default:
@ -141,6 +148,17 @@ public final class BooleanExpressionComplexityCheck extends Check
}
}
/**
* Checks if logical operator is part of constructor or method call.
* @param logicalOperator logical operator
* @return true if logical operator is part of constructor or method call
*/
private boolean isPassedInParameter(DetailAST logicalOperator)
{
return logicalOperator.getParent().getType() == TokenTypes.EXPR
&& logicalOperator.getParent().getParent().getType() == TokenTypes.ELIST;
}
/**
* Checks if {@link TokenTypes#BOR binary OR} is applied to exceptions
* in

View File

@ -37,6 +37,8 @@ public class BooleanExpressionComplexityCheckTest extends BaseCheckTestSupport
String[] expected = {
"13:9: " + getCheckMessage(MSG_KEY, 4, 3),
"32:9: " + getCheckMessage(MSG_KEY, 6, 3),
"38:34: " + getCheckMessage(MSG_KEY, 4, 3),
"40:34: " + getCheckMessage(MSG_KEY, 4, 3),
};
verify(checkConfig, getPath("metrics" + File.separator + "BooleanExpressionComplexityCheckTestInput.java"), expected);

View File

@ -31,4 +31,41 @@ public class BooleanExpressionComplexityCheckTestInput {
{
return (((_a & (_b & _c)) | (_c ^ _d) | (_a & _d)));
}
public void notIgnoredMethodParameters()
{
new Settings(Settings.FALSE && Settings.FALSE && Settings.FALSE
&& Settings.TRUE && Settings.TRUE);
new Settings(Settings.FALSE || Settings.FALSE || Settings.FALSE
|| Settings.TRUE || Settings.TRUE);
}
public void ignoredMethodParameters()
{
new Settings(Settings.RESIZABLE | Settings.SCROLLBARS | Settings.LOCATION_BAR
| Settings.MENU_BAR | Settings.TOOL_BAR);
new Settings(Settings.RESIZABLE & Settings.SCROLLBARS & Settings.LOCATION_BAR
& Settings.MENU_BAR & Settings.TOOL_BAR);
new Settings(Settings.RESIZABLE ^ Settings.SCROLLBARS ^ Settings.LOCATION_BAR
^ Settings.MENU_BAR ^ Settings.TOOL_BAR);
}
private class Settings {
public final static int RESIZABLE = 1;
public final static int SCROLLBARS = 2;
public final static int LOCATION_BAR = 3;
public final static int MENU_BAR = 4;
public final static int TOOL_BAR = 5;
public final static boolean TRUE = true;
public final static boolean FALSE = false;
public Settings(int flag)
{
}
public Settings(boolean flag)
{
}
}
}

View File

@ -28,9 +28,15 @@
Note that the operators <code>&#x26;</code> and
<code>|</code> are not only integer bitwise operators, they are also the
<a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.22.2">
non-shortcut versions</a> of the boolean operators
non-shortcut versions</a> of the boolean operators.
<code>&#x26;&#x26;</code> and <code>||</code>.
</p>
<p>
Note that <code>&#x26;</code>, <code>|</code> and <code>^</code> are not checked
if they are part of constructor or method call
because they can be applied to non boolean variables and
Checkstyle does not know types of methods from different classes.
</p>
</subsection>
<subsection name="Properties">