Merge branch 'pr-2679'

[java] InvalidLogMessageFormatRule throws IndexOutOfBoundsException when only logging exception message #2679
This commit is contained in:
Andreas Dangel
2020-07-31 10:52:56 +02:00
3 changed files with 28 additions and 2 deletions

View File

@ -89,9 +89,14 @@ public class InvalidLogMessageFormatRule extends AbstractJavaRule {
final List<ASTExpression> argumentList = parentNode.getFirstChildOfType(ASTPrimarySuffix.class)
.getFirstDescendantOfType(ASTArgumentList.class).findChildrenOfType(ASTExpression.class);
// ignore the first argument if it is a known non-string value, e.g. a slf4j-Marker
if (argumentList.get(0).getType() != null && !argumentList.get(0).getType().equals(String.class)) {
argumentList.remove(0);
if (argumentList.size() == 1) {
// no need to check for message params in case no string and no params found
return data;
} else {
// ignore the first argument if it is a known non-string value, e.g. a slf4j-Marker
argumentList.remove(0);
}
}
// remove the message parameter

View File

@ -865,6 +865,25 @@ class InvalidLogMessageFormatTest {
private static void foo(Consumer<String> consumer) {
consumer.accept("bar");
}
}
]]></code>
</test-code>
<test-code>
<description>#2431 IndexOutOfBoundsException when only logging exception message</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Foo {
private static final Logger LOG = LoggerFactory.getLogger(Foo.class);
public void bar() {
try {
new File("/text.txt");
} catch (Exception e) {
LOG.warn(e.getMessage());
}
}
}
]]></code>
</test-code>