Merge branch 'pr-2522'

[java] AvoidPrintStackTrace - consider method calls 
This commit is contained in:
Andreas Dangel
2020-05-22 20:15:41 +02:00
3 changed files with 63 additions and 3 deletions
docs/pages
pmd-java/src
main/resources/category/java
test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml

@ -52,6 +52,7 @@ This is useful to find duplicated sections in XML files.
* java-bestpractices
* [#2145](https://github.com/pmd/pmd/issues/2145): \[java] Deprecate rules PositionLiteralsFirstIn(CaseInsensitive)Comparisons in favor of LiteralsFirstInComparisons
* [#2288](https://github.com/pmd/pmd/issues/2288): \[java] JUnitTestsShouldIncludeAssert: Add support for Hamcrest MatcherAssert.assertThat
* [#2437](https://github.com/pmd/pmd/issues/2437): \[java] AvoidPrintStackTrace can't detect the case e.getCause().printStackTrace()
* java-codestyle
* [#2476](https://github.com/pmd/pmd/pull/2476): \[java] MethodNamingConventions - Add support for JUnit 5 method naming
* java-errorprone

@ -185,9 +185,11 @@ Avoid printStackTrace(); use a logger call instead.
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression
[PrimaryPrefix/Name[contains(@Image,'printStackTrace')]]
[PrimarySuffix[not(boolean(Arguments/ArgumentList/Expression))]]
//PrimaryExpression[
( PrimaryPrefix[Name[contains(@Image,'printStackTrace')]]
| PrimarySuffix[@Image='printStackTrace']
)/following-sibling::*[1][self::PrimarySuffix/Arguments[@ArgumentCount=0]]
]
]]>
</value>
</property>

@ -7,6 +7,7 @@
<test-code>
<description>simple failure</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>4</expected-linenumbers>
<code><![CDATA[
public class Foo {
void bar() {
@ -30,6 +31,62 @@ public class Foo {
LOG.error(e, "Whoa!");
}
}
}
]]></code>
</test-code>
<test-code>
<description>pos in call chain #2437</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
try {} catch (Exception e) {
e.getCause().printStackTrace();
}
}
}
]]></code>
</test-code>
<test-code>
<description>neg, not a real call</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
try {} catch (Exception e) {
e.getCause().printStackTrace;
}
}
}
]]></code>
</test-code>
<test-code>
<description>neg, not a real call 2</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
try {} catch (Exception e) {
e.printStackTrace;
}
}
}
]]></code>
</test-code>
<test-code>
<description>neg, not a real call 3</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
try {} catch (Exception e) {
e.printStackTrace(e);
}
}
}
]]></code>
</test-code>