Issue pmd#1035: ReturnFromFinallyBlock false-positives.
This commit is contained in:
@ -2790,7 +2790,7 @@ Avoid returning from a finally block, this can discard exceptions.
|
|||||||
|
|
||||||
**This rule is defined by the following XPath expression:**
|
**This rule is defined by the following XPath expression:**
|
||||||
``` xpath
|
``` xpath
|
||||||
//FinallyStatement//ReturnStatement
|
//FinallyStatement//ReturnStatement except //FinallyStatement//(MethodDeclaration|LambdaExpression)//ReturnStatement
|
||||||
```
|
```
|
||||||
|
|
||||||
**Example(s):**
|
**Example(s):**
|
||||||
|
@ -2666,8 +2666,9 @@ Avoid returning from a finally block, this can discard exceptions.
|
|||||||
</description>
|
</description>
|
||||||
<priority>3</priority>
|
<priority>3</priority>
|
||||||
<properties>
|
<properties>
|
||||||
|
<property name="version" value="2.0"/>
|
||||||
<property name="xpath">
|
<property name="xpath">
|
||||||
<value>//FinallyStatement//ReturnStatement</value>
|
<value>//FinallyStatement//ReturnStatement except //FinallyStatement//(MethodDeclaration|LambdaExpression)//ReturnStatement</value>
|
||||||
</property>
|
</property>
|
||||||
</properties>
|
</properties>
|
||||||
<example>
|
<example>
|
||||||
|
@ -54,6 +54,65 @@ public class Foo {
|
|||||||
} finally {
|
} finally {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
]]></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>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
Reference in New Issue
Block a user