Repository navigation
[java] Fix #4815: ExceptionAsFlowControl: stop at a lambda body - #7105
Conversation
An exception thrown in a lambda body propagates to whoever invokes the lambda, not to the try statement around the lambda expression. Fixes pmd#4815
|
Compared to main: (comment created at 2026-09-26 09:33:55+00:00 for 817461b) |
|
I looked into the false negative you pointed at. It reproduces on the merged rule: on Filed #7117 with a fix that keeps the walk going through lambdas passed to the JDK methods whose contract says the action runs in the calling thread; every other lambda stays opaque. #7118 is the PR. |
…hod it is passed to pmd#7105 stopped the walk at every lambda body. That fixed the false positive of pmd#4815, but it also stopped reporting a throw inside a lambda that the method it is passed to calls itself. spring-framework's LeakAwareNettyDataBufferFactory#checkForLeaks (v5.3.13, line 56) throws inside Iterable.forEach and catches the exception right below. The walk now continues through a lambda that is an argument of a call whose method calls that argument: Iterable.forEach, Iterator.forEachRemaining, Map.forEach, Optional.ifPresent/ifPresentOrElse, and the Stream methods that take a lambda argument. A terminal operation calls the action while the call is evaluated; an intermediate operation is evaluated by the terminal operation of the same pipeline. Every other lambda stays opaque, so pmd#4815 does not come back. Signed-off-by: Eljees <[email protected]>
…hod it is passed to (#7118) * [java] ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to #7105 stopped the walk at every lambda body. That fixed the false positive of #4815, but it also stopped reporting a throw inside a lambda that the method it is passed to calls itself. spring-framework's LeakAwareNettyDataBufferFactory#checkForLeaks (v5.3.13, line 56) throws inside Iterable.forEach and catches the exception right below. The walk now continues through a lambda that is an argument of a call whose method calls that argument: Iterable.forEach, Iterator.forEachRemaining, Map.forEach, Optional.ifPresent/ifPresentOrElse, and the Stream methods that take a lambda argument. A terminal operation calls the action while the call is evaluated; an intermediate operation is evaluated by the terminal operation of the same pipeline. Every other lambda stays opaque, so #4815 does not come back. Signed-off-by: Eljees <[email protected]> * Use compound invocation matcher and clarify release notes * [java] Handle primitive streams in ExceptionAsFlowControl --------- Signed-off-by: Eljees <[email protected]>
Describe the PR
ExceptionAsFlowControlwalks up from athrowto the enclosing body declaration and reports anytryon the way whosecatchwould catch the thrown type. The walk did not stop at a lambda expression, so in the code from #4815the
throwinside the lambda was matched against the outercatch (RuntimeException e). Thatcatchis not where the exception goes: the lambda body runs when the executor invokes it, on another thread here, and the exception propagates to that caller.The rule already stops at a body declaration, which covers anonymous and local classes. This makes it stop at a lambda too: whoever invokes the lambda is not visible from the throw site, so the rule cannot tell whether a surrounding
trycatches the exception, and it now leaves that case alone. +7/-0 inExceptionAsFlowControlRule.Tests
Two cases in
ExceptionAsFlowControl.xml:mainwithExpected 0 problem(s), 1 problem(s) found;try, with a lambda in it and athrowoutside the lambda: still reported (line 12, thecatch), so a lambda elsewhere in thetrydoes not switch the rule off.ExceptionAsFlowControlTest: 9 tests, 0 failures.Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)