[java] GuardLogStatementRule - fix false negative with lambda

This commit is contained in:
Andreas Dangel committed 2021-05-20 16:50:40 +02:00
1 parent 8229ef7c44
commit d51ceda978
2 files changed
+37 -2

No files matched your search

@@ -150,7 +150,17 @@ public class GuardLogStatementRule extends AbstractJavaRule implements Rule {
return !isConstantStringExpression(child);
}
}
return false;
// if there is only one argument and this is a AdditiveExpression, we assume, it is the message
// and this is a string concatenation even we are not sure, that the type is string.
// this can happen for lambda parameters.
return isSingleAdditiveExpression(argumentList);
}
private boolean isSingleAdditiveExpression(ASTArgumentList argumentList) {
return argumentList.size() == 1
&& argumentList.getChild(0).getNumChildren() == 1
&& argumentList.getChild(0).getChild(0) instanceof ASTAdditiveExpression;
}
private boolean isConstantStringExpression(JavaNode expr) {
@@ -507,12 +507,37 @@ public class Logger {
private void runTestLambda(TestLambda test) {
test.apply("one", "two");
}
}
]]></code>
</test-code>
public void case6() {
<test-code>
<description>false negative inside lambdas</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>19,21</expected-linenumbers>
<code><![CDATA[
public class Logger {
private static final Logger LOGGER = new Logger();
private interface TestLambda {
void apply(String arg1, String arg2);
}
private void runTestLambda(TestLambda test) {
test.apply("one", "two");
}
public void case1_no_violation() {
runTestLambda((String a, String b) -> {
LOGGER.debug(a);
});
}
public void case2_violation() {
runTestLambda((String a, String b) -> {
LOGGER.debug(a + b);
});
runTestLambda((s1, s2) -> {LOGGER.debug(s1 + s2);});
}
}
]]></code>
</test-code>