Merge branch 'pr-1503'

This commit is contained in:
Andreas Dangel
2018-12-13 08:38:09 +01:00
4 changed files with 98 additions and 11 deletions

View File

@ -2790,7 +2790,7 @@ Avoid returning from a finally block, this can discard exceptions.
**This rule is defined by the following XPath expression:**
``` xpath
//FinallyStatement//ReturnStatement
//FinallyStatement//ReturnStatement except //FinallyStatement//(MethodDeclaration|LambdaExpression)//ReturnStatement
```
**Example(s):**

View File

@ -18,11 +18,14 @@ This is a {{ site.pmd.release_type }} release.
* java-bestpractices
* [#658](https://github.com/pmd/pmd/issues/658): \[java] OneDeclarationPerLine: False positive for loops
* java-errorprone
* [#1035](https://github.com/pmd/pmd/issues/1035): \[java] ReturnFromFinallyBlock: False positive on lambda expression in finally block
### API Changes
### External Contributions
* [#1503](https://github.com/pmd/pmd/pull/1503): \[java] Fix for ReturnFromFinallyBlock false-positives - [RishabhDeep Singh](https://github.com/rishabhdeepsingh)
* [#1516](https://github.com/pmd/pmd/pull/1516): \[java] OneDeclarationPerLine: Don't report multiple variables in a for statement. - [Kris Scheibe](https://github.com/kris-scheibe)
* [#1521](https://github.com/pmd/pmd/pull/1521): \[java] Upgrade to ASM7 for JDK 11 support - [Mark Pritchard](https://github.com/markpritchard)

View File

@ -2666,8 +2666,9 @@ Avoid returning from a finally block, this can discard exceptions.
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>//FinallyStatement//ReturnStatement</value>
<value>//FinallyStatement//ReturnStatement except //FinallyStatement//(MethodDeclaration|LambdaExpression)//ReturnStatement</value>
</property>
</properties>
<example>

View File

@ -4,9 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description><![CDATA[
throw exception but return from finally
]]></description>
<description>throw exception but return from finally</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
@ -23,9 +21,7 @@ public class Foo {
]]></code>
</test-code>
<test-code>
<description><![CDATA[
lots of returns
]]></description>
<description>lots of returns</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
@ -42,9 +38,7 @@ public class Foo {
]]></code>
</test-code>
<test-code>
<description><![CDATA[
ok
]]></description>
<description>ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
@ -57,4 +51,93 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>#1035 [java] ReturnFromFinallyBlock: False positive on lambda expression in finally block</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
String bar() {
try {
} finally {
Object o = new Object() {
@Override
public String toString() {
return "";
}
};
}
}
}
]]></code>
</test-code>
<test-code>
<description>Return from Lambda in Finally</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
String getBar() {
try {
} finally {
Collection<ServiceExecutionEntry> untracked = serviceExecutionTracker.untrackMatchingEntries(e -> {
ServiceExecutionIdentifiers ids = e.getIdentifiers();
Long execBqiId = (ids == null) ? null : ids.getBqiId();
return Objects.equals(bqiId, execBqiId);
});
untracked.forEach(e -> logger.info("overwriteLastBqi(bqId={}, bqiId={}) untracked {}", bqId, bqiId, e));
}
}
}
]]></code>
</test-code>
<test-code>
<description>return from a lambda should not cause issues</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void printSomething() {
try {
System.out.println("Integers: ");
} finally {
Arrays.asList(0, 1, 2).map(i -> { return i + 1; }).forEach(i -> System.out.println(i));
}
}
}
]]></code>
</test-code>
<test-code>
<description>Return in finally should give error but not in lambda</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
String printSomething() {
try {
System.out.println("Integers: ");
} finally {
Arrays.asList(0, 1, 2).map(i -> { return i + 1; }).forEach(i -> System.out.println(i));
return "foo";
}
}
}
]]></code>
</test-code>
<test-code>
<description>Return from anonyous class should not give any error</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void printSomething() {
try {
System.out.println("Integers: ");
} finally {
Arrays.asList(0, 1, 2).map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer i) {
return i + 1;
}
}).forEach(i -> System.out.println(i));
}
}
}
]]></code>
</test-code>
</test-data>