Merge branch 'pr-2679'
[java] InvalidLogMessageFormatRule throws IndexOutOfBoundsException when only logging exception message #2679
This commit is contained in:
@ -22,6 +22,7 @@ This is a {{ site.pmd.release_type }} release.
|
||||
* java-bestpractices
|
||||
* [#2668](https://github.com/pmd/pmd/issues/2668): \[java] UnusedAssignment false positives
|
||||
* java-errorprone
|
||||
* [#2431](https://github.com/pmd/pmd/issues/2431): \[java] InvalidLogMessageFormatRule throws IndexOutOfBoundsException when only logging exception message
|
||||
* [#2439](https://github.com/pmd/pmd/issues/2439): \[java] AvoidCatchingThrowable can not detect the case: catch (java.lang.Throwable t)
|
||||
* java-performance
|
||||
* [#2441](https://github.com/pmd/pmd/issues/2441): \[java] RedundantFieldInitializer can not detect a special case for char initialize: `char foo = '\0';`
|
||||
@ -44,6 +45,7 @@ This is a {{ site.pmd.release_type }} release.
|
||||
|
||||
* [#2677](https://github.com/pmd/pmd/pull/2677): \[java] RedundantFieldInitializer can not detect a special case for char initialize: `char foo = '\0';` - [Mykhailo Palahuta](https://github.com/Drofff)
|
||||
* [#2678](https://github.com/pmd/pmd/pull/2678): \[java] AvoidCatchingThrowable can not detect the case: catch (java.lang.Throwable t) - [Mykhailo Palahuta](https://github.com/Drofff)
|
||||
* [#2679](https://github.com/pmd/pmd/pull/2679): \[java] InvalidLogMessageFormatRule throws IndexOutOfBoundsException when only logging exception message - [Mykhailo Palahuta](https://github.com/Drofff)
|
||||
|
||||
|
||||
{% endtocmaker %}
|
||||
|
@ -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
|
||||
|
@ -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>
|
||||
|
Reference in New Issue
Block a user