Skip to content

Keep calls that may throw inside the AssertThrowsOnLastStatement lambda - #1306

Merged
timtebeek merged 2 commits into
mainfrom
tim/openrewrite-issue-1211-propose
Nov 17, 2026
Merged

timtebeek merged 2 commits into
mainfrom
tim/openrewrite-issue-1211-propose

Conversation

@timtebeek

Copy link
Copy Markdown
Member

Addresses C14 in #1211, including the case reported in #1211 (comment). AssertThrowsOnLastStatement moved calls out of the assertThrows lambda without checking whether they throw. In the IOUtilTest case from apache/maven-shared-utils, IOUtil.toString(..., "junk") was moved out of the last statement's arguments. The test no longer compiled, because the call throws a checked UnsupportedEncodingException. The same happened to earlier statements in the block.

A call now stays in the lambda when it declares the expected exception, a subclass of it, or a parent class of it. In Java, a call also stays when it declares a checked exception the test method doesn't. Calls without a method type count as possibly throwing. A flagged argument of the last statement stays inline while the other arguments are still moved out. A flagged earlier statement leaves the whole assertThrows unchanged.

Also in this PR:

  • Moved-out variables use their real type, so arrays become byte[] bytes instead of Object bytes. Types that can't be written out, such as generic type variables, stay inline instead of being declared as Object.
  • An empty () -> {} lambda no longer deletes the assertThrows statement.
  • Removed an @Issue link to a non-existent issue number.
  • Added a regression test showing an unrelated block lambda is left alone. That side effect was already fixed in Run unconditional sub-recipes from getRecipeList() and keep cleanups local to the changed element #1230.

The C14 suspicion of duplicate names from sibling assertThrows calls is not addressed here. The recipe description still warns that earlier statements can throw; that remains true for unchecked exceptions a method doesn't declare.

Verified with ./gradlew test: 2556 tests, 0 failures. The six new tests in AssertThrowsOnLastStatementTest cover the reported case and the cases above.

@timtebeek
timtebeek marked this pull request as ready for review November 17, 2026 19:36
@timtebeek

Copy link
Copy Markdown
Member Author

Thanks again for the report @slachiewicz! Release should go out tonight.

@timtebeek
timtebeek merged commit 6f9ae9c into main Nov 17, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/openrewrite-issue-1211-propose branch November 17, 2026 19:37
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant