Skip to content

Commit

Permalink
Issue sevntu-checkstyle#454: fixed NPE in ConfusingConditionCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and kariem committed Jul 26, 2018
1 parent 3884e3a commit cdfc539
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 15 deletions.
Expand Up @@ -48,7 +48,7 @@
* and swapped code in "if" and "else" block:
* </p>
* <pre>
* if (a == b &amp;&amp; c == d)
* if (a == b || c == d)
* {
* smth2();
* }
Expand Down Expand Up @@ -246,22 +246,50 @@ private boolean isRatioBetweenIfAndElseBlockSuitable(DetailAST literalIf) {
* @return linesOfCodeInIfBlock line of code in block.
*/
private static int getAmounOfCodeRowsInBlock(DetailAST detailAST) {
final DetailAST firstBrace = getFirstBrace(detailAST);
int linesOfCodeInIfBlock;

if (firstBrace == null) {
linesOfCodeInIfBlock = 0;
}
else {
final DetailAST lastBrace = firstBrace.getLastChild();
linesOfCodeInIfBlock = lastBrace.getLineNo()
- firstBrace.getLineNo();
// If the closing brace on a separate line - ignore this line.
if (lastBrace.getLineNo() != lastBrace.getParent().getLineNo()) {
linesOfCodeInIfBlock -= 1;
}
}

return linesOfCodeInIfBlock;
}

/**
* Retrieves the first, opening brace of an {@code if} or {@code else} statement.
* @param detailAST The token to examine.
* @return The opening brace token or {@code null} if it doesn't exist.
*/
private static DetailAST getFirstBrace(DetailAST detailAST) {
DetailAST firstBrace = null;

if (detailAST.getType() == TokenTypes.LITERAL_ELSE) {
firstBrace = detailAST.getFirstChild();

if (firstBrace.getType() == TokenTypes.LITERAL_IF) {
firstBrace = getFirstBrace(firstBrace);
}
}
else {
firstBrace = detailAST.getFirstChild().getNextSibling()
.getNextSibling().getNextSibling();
}
final DetailAST lastBrace = firstBrace.getLastChild();
int linesOfCodeInIfBlock = lastBrace.getLineNo()
- firstBrace.getLineNo();
// If the closing brace on a separate line - ignore this line.
if (lastBrace.getLineNo() != lastBrace.getParent().getLineNo()) {
linesOfCodeInIfBlock -= 1;

if (firstBrace != null && firstBrace.getType() != TokenTypes.SLIST) {
firstBrace = null;
}
return linesOfCodeInIfBlock;

return firstBrace;
}

/**
Expand Down
Expand Up @@ -37,12 +37,6 @@ public class ConfusingConditionCheckTest extends BaseCheckTestSupport {
public void testDefault() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(ConfusingConditionCheck.class);

checkConfig.addAttribute("ignoreInnerIf", "true");
checkConfig.addAttribute("ignoreNullCaseInIf", "true");
checkConfig.addAttribute("ignoreSequentialIf", "true");
checkConfig.addAttribute("ignoreThrowInElse", "true");
checkConfig.addAttribute("multiplyFactorForElseBlocks", "4");

final String[] expected = {
"10: " + warningMessage,
"13: " + warningMessage,
Expand All @@ -59,6 +53,8 @@ public void testDefault() throws Exception {
"200: " + warningMessage,
"215: " + warningMessage,
"231: " + warningMessage,
"250: " + warningMessage,
"259: " + warningMessage,
};

verify(checkConfig, getPath("InputConfusingConditionCheck.java"),
Expand Down Expand Up @@ -99,6 +95,10 @@ public void testFalseProperties() throws Exception {
"215: " + warningMessage,
"227: " + warningMessage,
"231: " + warningMessage,
"247: " + warningMessage,
"250: " + warningMessage,
"257: " + warningMessage,
"259: " + warningMessage,
};

verify(checkConfig, getPath("InputConfusingConditionCheck.java"),
Expand All @@ -121,9 +121,20 @@ public void testMultiplyFactor() throws Exception {
"108: " + warningMessage,
"111: " + warningMessage,
"177: " + warningMessage,
"259: " + warningMessage,
};

verify(checkConfig, getPath("InputConfusingConditionCheck.java"),
expected);
}

@Test
public void testExceptions() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(ConfusingConditionCheck.class);

final String[] expected = {};

verify(checkConfig, getPath("InputConfusingConditionCheck2.java"),
expected);
}
}
Expand Up @@ -242,7 +242,24 @@ else if (!a) {}
}
else {
a = true;
}
}

if (!a) {
//
}
else if (!b) {
a = true;
}
else {
//
}

if (!a)
;
else if (!b)
;
else
;

}
}
@@ -0,0 +1,17 @@
package com.github.sevntu.checkstyle.checks.coding;

public class InputConfusingConditionCheck2 {
class InnerEmptyBlocks {
void foo() {
}
}

InnerEmptyBlocks anon = new InnerEmptyBlocks() {
boolean flag = true;

void foo() {
if(flag);
else;
}
};
}

0 comments on commit cdfc539

Please sign in to comment.