Issue pmd#1035: ReturnFromFinallyBlock false-positives.

This commit is contained in:
Rishabh Deep Singh
2018-12-05 00:26:18 +05:30
parent 178c9a8199
commit 83f8f1b410
3 changed files with 62 additions and 2 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

@ -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

@ -57,4 +57,63 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Return from a Class in Finally
]]></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><![CDATA[
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><![CDATA[
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-data>