Repository navigation
[java] Fix #7117: ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to - #7118
Conversation
|
Compared to main: (comment created at 2026-09-30 17:50:47+00:00 for dfab14b) |
|
Is this really about "in the same thread"? I'd say it's more about the fact that the lambda gets called by the method its passed to (e.g. forEach). |
|
Probably all methods of |
…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]>
0b03905 to
4e64d1a
Compare
|
Both review points are addressed and pushed:
|
| * The Java rule {% rule java/design/ExceptionAsFlowControl %} now reports exceptions thrown inside a lambda | ||
| that the method it is passed to calls itself, such as `Iterable.forEach` or a `Stream` operation that | ||
| takes a lambda. Exceptions thrown inside any other lambda are still not reported. |
There was a problem hiding this comment.
It did so in the current, release version as well. So this isn't really a change.
| private static final Set<String> CALLS_THE_LAMBDA_ARGUMENT = new HashSet<>(Arrays.asList( | ||
| "java.lang.Iterable#forEach", | ||
| "java.util.Iterator#forEachRemaining", | ||
| "java.util.Map#forEach", | ||
| "java.util.Optional#ifPresent", | ||
| "java.util.Optional#ifPresentOrElse", | ||
| "java.util.stream.Stream#allMatch", | ||
| "java.util.stream.Stream#anyMatch", | ||
| "java.util.stream.Stream#collect", | ||
| "java.util.stream.Stream#dropWhile", | ||
| "java.util.stream.Stream#filter", | ||
| "java.util.stream.Stream#flatMap", | ||
| "java.util.stream.Stream#flatMapToDouble", | ||
| "java.util.stream.Stream#flatMapToInt", | ||
| "java.util.stream.Stream#flatMapToLong", | ||
| "java.util.stream.Stream#forEach", | ||
| "java.util.stream.Stream#forEachOrdered", | ||
| "java.util.stream.Stream#generate", | ||
| "java.util.stream.Stream#iterate", | ||
| "java.util.stream.Stream#map", | ||
| "java.util.stream.Stream#mapMulti", | ||
| "java.util.stream.Stream#mapMultiToDouble", | ||
| "java.util.stream.Stream#mapMultiToInt", | ||
| "java.util.stream.Stream#mapMultiToLong", | ||
| "java.util.stream.Stream#mapToDouble", | ||
| "java.util.stream.Stream#mapToInt", | ||
| "java.util.stream.Stream#mapToLong", | ||
| "java.util.stream.Stream#max", | ||
| "java.util.stream.Stream#min", | ||
| "java.util.stream.Stream#noneMatch", | ||
| "java.util.stream.Stream#peek", | ||
| "java.util.stream.Stream#reduce", | ||
| "java.util.stream.Stream#sorted", | ||
| "java.util.stream.Stream#takeWhile" | ||
| )); |
There was a problem hiding this comment.
This really looks like a CompoundInvocationMatcher. Would also simplify lines 131-135.
|
Replaced the manual declaring-type/method-name lookup with CompoundInvocationMatcher and its anyMatch call. Removed the Changed Rules entry and retained the Fixed Issues entry. Rebuilt the Java module and ran ExceptionAsFlowControlTest: all 13 tests pass. |
Describe the PR
#4815 was a false positive: a
throwinside a lambda passed to an executor was matched against thetryaround the call. #7105 stopped the walk at every lambda body, which fixed that, and left a false negative behind - pointed out by @UncleOwen in the review of #7105, with spring-framework'sLeakAwareNettyDataBufferFactory#checkForLeaks(v5.3.13, line 56) as the example:Iterable.forEachcalls the lambda it is passed and relays an exception thrown by it to the caller, so thatthrowis caught by thecatchright below it - the control flow this rule is about. Per @UncleOwen, the criterion is the contract of the method the lambda is passed to. @zbynek added that all methods ofStreamthat take a lambda should be treated the same.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 everyStreammethod that takes 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. +75/-3 inExceptionAsFlowControlRule.Tests
Four cases added to
ExceptionAsFlowControl.xml:catch. With the rule as onmainthe same fixture fails withExpected 1 problem(s), 0 problem(s) found;forEach, but the exception type is not caught there: no violation, so the rule is not switched off insideforEach;Stream.mappipeline inside thetry: one violation on thecatch;map, but the exception type is not caught there: no violation.The two cases from #4815 are unchanged.
ExceptionAsFlowControlTest: 13 tests, 0 failures. With the rule as on currentmain(7774e57) the same fixtures fail in two cases, withExpected 1 problem(s), 0 problem(s) found.The rule was also run through the PMD API on the whole spring-framework file (127 lines): no violation with the rule as on
main, one violation at line 61 with this patch.The branch is rebased on current
main;docs/pages/release_notes.mdlists the issue under Fixed Issues and the new reporting behaviour under Changed Rules.Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)