Skip to content

Commit

Permalink
Issue #922: fixes NPE from pattern definition for LogicNeedOptimization
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed Oct 28, 2022
1 parent 8546ead commit df7fe8c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private static DetailAST[] getOperands(DetailAST logicNode) {
int parenthesis = 0;

do {
if (node.getType() == TokenTypes.LPAREN) {
if (node.getType() == TokenTypes.LPAREN && node.getFirstChild() == null) {
parenthesis++;
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ public void test() throws Exception {
verify(checkConfig, getPath("InputLogicConditionNeedOptimizationCheck.java"), expected);
}

@Test
public void testPatternDefinition() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(LogicConditionNeedOptimizationCheck.class);
final String[] expected = {
"32:17: " + getCheckMessage(MSG_KEY, "&&", 32, 16),
"66:34: " + getCheckMessage(MSG_KEY, "&&", 66, 33),
};
verify(checkConfig, getNonCompilablePath("InputLogicConditionNeedOptimizationCheck2.java"),
expected);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.github.sevntu.checkstyle.checks.coding;

public class InputLogicConditionNeedOptimizationCheck2 {
String typeGuardAfterParenthesizedTrueSwitchStatement1(Object o) {
switch (o) {
case (Integer i)
&& i == 0: o = String.valueOf(i); return "true";
case ((Integer i)
&& i == 2): o = String.valueOf(i); return "second";
case Object x: return "any";
}
}

String typeGuardAfterParenthesizedTrueSwitchExpression1(Object o) {
return switch (o) {
case (Integer i)
&& i == 0: o = String.valueOf(i); yield "true";
case ((Integer i)
&& i == 2): o = String.valueOf(i); yield "second";
case Object x: yield "any";
};
}

String typeGuardAfterParenthesizedTrueIfStatement1(Object o) {
if (o != null
&& o instanceof ((Integer i)
&& i == 0)) {
return "true";
} else if (o != null
&& o instanceof (((Integer i)
&& i == 2))
&& (o = i) != null) { // violation
return "second";
} else {
return "any";
}
}

String typeGuardAfterParenthesizedTrueSwitchStatement2(Object o) {
switch (o) {
case (Integer i) &&
i == 0: o = String.valueOf(i); return "true";
case ((Integer i) &&
i == 2): o = String.valueOf(i); return "second";
case Object x: return "any";
}
}

String typeGuardAfterParenthesizedTrueSwitchExpression2(Object o) {
return switch (o) {
case (Integer i) &&
i == 0: o = String.valueOf(i); yield "true";
case ((Integer i) &&
i == 2): o = String.valueOf(i); yield "second";
case Object x: yield "any";
};
}

String typeGuardAfterParenthesizedTrueIfStatement2(Object o) {
if (o != null &&
o instanceof ((Integer i) &&
i == 0)) {
return "true";
} else if (o != null &&
o instanceof (((Integer i) &&
i == 2)) && // violation
(o = i) != null) {
return "second";
} else {
return "any";
}
}
}

0 comments on commit df7fe8c

Please sign in to comment.