Skip to content

Commit

Permalink
Merge d4e5665 into f62e3f1
Browse files Browse the repository at this point in the history
  • Loading branch information
nrmancuso committed May 24, 2021
2 parents f62e3f1 + d4e5665 commit 08d9928
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FullIdent;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CheckUtil;

Expand Down Expand Up @@ -80,9 +82,9 @@ public class AvoidNotShortCircuitOperatorsForBooleanCheck extends AbstractCheck
public static final String MSG_KEY = "avoid.not.short.circuit.operators.for.boolean";

/**
* A "boolean" String.
* */
private static final String BOOLEAN = "boolean";
* Pattern to match boolean types, including array types.
*/
private static final Pattern BOOLEAN_TYPE_PATTERN = Pattern.compile("^boolean(\\[[^]]*])*");

/**
* A list contains all names of operands, which are used in the current
Expand Down Expand Up @@ -140,8 +142,11 @@ public final void visitToken(final DetailAST detailAST) {
* @return "true" if current method or variable has a Boolean type.
*/
private static boolean isBooleanType(final DetailAST node) {
return BOOLEAN.equals(CheckUtil.createFullType(
node.findFirstToken(TokenTypes.TYPE)).getText());
final FullIdent methodOrVariableType =
CheckUtil.createFullType(node.findFirstToken(TokenTypes.TYPE));
return BOOLEAN_TYPE_PATTERN
.matcher(methodOrVariableType.getText())
.find();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,11 @@ && isReturnedValueBeNull(detailAST))) {
* @return true, when method return collection.
*/
private boolean isReturnCollection(DetailAST methodDef) {
DetailAST methodType = methodDef.findFirstToken(TokenTypes.TYPE);
methodType = methodType.getFirstChild();
return methodType.getType() == TokenTypes.ARRAY_DECLARATOR
|| collectionList.contains(methodType.getText());
final DetailAST methodType = methodDef.findFirstToken(TokenTypes.TYPE);
final boolean isArrayType =
methodType.findFirstToken(TokenTypes.ARRAY_DECLARATOR) != null;
return isArrayType
|| collectionList.contains(methodType.getFirstChild().getText());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,16 @@ public final void testExpressionBeforeTry() throws Exception {
CommonUtil.EMPTY_STRING_ARRAY);
}

@Test
public final void testBitwiseOrAfterArrayIndex() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(AvoidNotShortCircuitOperatorsForBooleanCheck.class);
final String[] expected = {
"6:19: " + getCheckMessage(MSG_KEY, "|="),
"8:20: " + getCheckMessage(MSG_KEY, "|="),
};
verify(checkConfig,
getPath("InputAvoidNotShortCircuitOperatorsForBooleanCheckBitwiseAfterArray.java"),
expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.sevntu.checkstyle.checks.coding;

public class InputAvoidNotShortCircuitOperatorsForBooleanCheckBitwiseAfterArray {
void boolFalse(String[] args) {
final boolean[] values = {false};
values[0] |= (args.length > 0);
final boolean[][] val2 = {{false}, {false}};
val2[0][0] |= (args.length > 0);
}
}

0 comments on commit 08d9928

Please sign in to comment.