Skip to content

Commit

Permalink
Increase AvoidNotShortCircuitOperatorsForBooleanCheck code coverage
Browse files Browse the repository at this point in the history
Pull #324
  • Loading branch information
damianszczepanik authored and romani committed Mar 1, 2015
1 parent 1d7d081 commit c040d65
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 51 deletions.
1 change: 0 additions & 1 deletion sevntu-checks/pom.xml
Expand Up @@ -93,7 +93,6 @@
<regex><pattern>.*.checks.coding.AvoidDefaultSerializableInInnerClasses.*</pattern><branchRate>93</branchRate><lineRate>92</lineRate></regex>
<regex><pattern>.*.checks.coding.AvoidHidingCauseExceptionCheck</pattern><branchRate>87</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.coding.AvoidModifiersForTypesCheck</pattern><branchRate>96</branchRate><lineRate>98</lineRate></regex>
<regex><pattern>.*.checks.coding.AvoidNotShortCircuitOperatorsForBooleanCheck</pattern><branchRate>86</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.coding.ConfusingConditionCheck</pattern><branchRate>84</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.coding.CustomDeclarationOrderCheck.*</pattern><branchRate>81</branchRate><lineRate>83</lineRate></regex>
<regex><pattern>.*.checks.coding.DiamondOperatorForVariableDefinitionCheck</pattern><branchRate>90</branchRate><lineRate>100</lineRate></regex>
Expand Down
Expand Up @@ -115,27 +115,18 @@ public final void visitToken(final DetailAST detailAST)
{

DetailAST currentNode = detailAST;
while (currentNode != null
&& currentNode.getType() != TokenTypes.EXPR
&& currentNode.getType() != TokenTypes.METHOD_DEF
&& currentNode.getType() != TokenTypes.CTOR_DEF
&& currentNode.getType() != TokenTypes.CLASS_DEF)
// look for EXPR which is always around BOR/BAND... operators
while (currentNode.getType() != TokenTypes.EXPR)
{
currentNode = currentNode.getParent();
}

final int type = currentNode.getType();

if (type == TokenTypes.EXPR) {

if (isBooleanExpression(currentNode)) {
log(detailAST, MSG_KEY, detailAST.getText());
}

supportedOperands.clear();
hasTrueOrFalseLiteral = false;
if (isBooleanExpression(currentNode)) {
log(detailAST, MSG_KEY, detailAST.getText());
}

supportedOperands.clear();
hasTrueOrFalseLiteral = false;
}

/**
Expand All @@ -160,14 +151,12 @@ public final static boolean isBooleanType(final DetailAST node)
*/
public final boolean isBooleanExpression(final DetailAST node)
{

DetailAST curNode = node;

final List<String> childNames = getSupportedOperandsNames(curNode);
final List<String> booleanVariablesNames = new LinkedList<String>();

while (curNode != null
&& curNode.getType() != TokenTypes.CTOR_DEF
while (curNode.getType() != TokenTypes.CTOR_DEF
&& curNode.getType() != TokenTypes.METHOD_DEF
&& curNode.getType() != TokenTypes.CLASS_DEF)
{
Expand Down Expand Up @@ -218,7 +207,6 @@ public final List<String> getSupportedOperandsNames(
}

if (currentNode.getType() == TokenTypes.IDENT
&& currentNode.getParent() != null
&& currentNode.getParent().getType() != TokenTypes.DOT)
{
supportedOperands.add(currentNode.getText());
Expand Down
Expand Up @@ -35,11 +35,12 @@ public final void testAll() throws Exception
{

String[] expected = {
"6:17: " + getCheckMessage(MSG_KEY, "|"),
"6:23: " + getCheckMessage(MSG_KEY, "|"),
"25:20: " + getCheckMessage(MSG_KEY, "|"),
"35:25: " + getCheckMessage(MSG_KEY, "|"),
"48:25: " + getCheckMessage(MSG_KEY, "|"),
"53:16: " + getCheckMessage(MSG_KEY, "&"),
"35:30: " + getCheckMessage(MSG_KEY, "|"),
"43:21: " + getCheckMessage(MSG_KEY, "|"),
"48:29: " + getCheckMessage(MSG_KEY, "|"),
"53:17: " + getCheckMessage(MSG_KEY, "&"),
"64:17: " + getCheckMessage(MSG_KEY, "|"),
"71:9: " + getCheckMessage(MSG_KEY, "|"),
"79:9: " + getCheckMessage(MSG_KEY, "|"),
Expand Down
@@ -1,20 +1,20 @@
package com.github.sevntu.checkstyle.checks.coding;

public class InputAvoidNotShortCircuitOperatorsForBooleanCheck {
public static boolean x;
public boolean y, z;
boolean result=x|y||z; // !
public static boolean x;
public boolean y, z;
boolean result = x|y||z; // !

public void main(){

boolean res1=x|y||z; //
int res2 = 4|56;
if(x|y||z){ //
int kkk=5;
}
boolean res1 = x|y||z; //
int res2 = 4|56|8|22;
if (x|y||z) { //
int kkk = 5;
}

x|=x&&y||z; //
x=x&y||z; //
x |= x&&y||z; //
x = x&y||z; //

x |= isFalse();
x = isFalse() | isFalse() & isTrue();
Expand All @@ -31,27 +31,27 @@ public int doSomething() {
}

boolean isFalse() {
boolean i=false;
for(int x=0; x<6|i; x|=5){ // !
int k=0;
boolean i = false;
for (int x = 0; x < 6|i; x |= 5) { // !
int k = 0;
}
int k=6;
int y=6;
while((k&y) > 7){ //
int h=0;
int k = 6;
int y = 6;
while ((k&y) > 7) { //
int h = 0;
}
return false;
return false|true;
}

boolean isGood() {
boolean i=true;
for(int x=0; x<6|i; x|=5){ // !
int k=0;
boolean i = true;
for(int x = 0; x < 6|i; x |= 5) { // !
int k = 0;
}
boolean k=true;
boolean y=false;
while(k&y){ // !
int h=0;
boolean k = true;
boolean y = false;
while (k&y) { // !
int h = 0;
}
return false;
}
Expand Down Expand Up @@ -99,8 +99,8 @@ public void check() {
boolean m = x|z; //
boolean m1 = x|y; //
x |= this.z; //
while(x|y){} //
boolean x=true;
while (x|y) {} //
boolean x = true;
y |= InputAvoidNotShortCircuitOperatorsForBooleanCheck.x; //

y |= getMessage(x);
Expand All @@ -125,5 +125,27 @@ public void doSomethingElse() {
while (x | y) {
}
}

public void invoker()
{
boolean x = this.y | someMethod(y | z);
}

public Object someConstructor()
{
return new MyConstructor(x | y);
}

public boolean someMethod(boolean value)
{
return !value;
}
}

class MyConstructor
{
MyConstructor(boolean expr)
{
boolean x = InputAvoidNotShortCircuitOperatorsForBooleanCheck.x | InputAvoidNotShortCircuitOperatorsForBooleanCheck.x;
}
}

0 comments on commit c040d65

Please sign in to comment.