Fix UnnecessaryCatch Recipe Multi-Catch Handling#638
Merged
timtebeek merged 20 commits intoopenrewrite:mainfrom Jul 17, 2025
Merged
Fix UnnecessaryCatch Recipe Multi-Catch Handling#638timtebeek merged 20 commits intoopenrewrite:mainfrom
UnnecessaryCatch Recipe Multi-Catch Handling#638timtebeek merged 20 commits intoopenrewrite:mainfrom
Conversation
* feat(junie): added .junie workflow * feat(junie): added .devcontainer.json * Update devcontainer.json --------- Co-authored-by: jetbrains-junie[bot] <201638009+jetbrains-junie[bot]@users.noreply.github.com> Co-authored-by: Knut Wannheden <[email protected]>
) This reverts commit ec88484.
Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.java.recipes.ExamplesExtractor?organizationId=ODQ2MGExMTUtNDg0My00N2EwLTgzMGMtNGE1NGExMTBmZDkw Co-authored-by: Moderne <[email protected]>
timtebeek
approved these changes
Jul 16, 2025
Member
timtebeek
left a comment
There was a problem hiding this comment.
Thanks for the detailed fixes here and link to the docs; great to have all of that in view when reviewing. I've added some polish to remove the use of JavaTemplate. That improves performance and ought to lead to a smaller change set in some edge cases, as we merely apply the filter step you described.
…java Co-authored-by: Stefano Dalla Palma <[email protected]>
src/main/java/org/openrewrite/staticanalysis/UnnecessaryCatch.java
Outdated
Show resolved
Hide resolved
…java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
UnnecessaryCatch Recipe Multi-Catch Handling
timtebeek
approved these changes
Jul 17, 2025
Member
timtebeek
left a comment
There was a problem hiding this comment.
Thanks for the help here @stefanodallapalma ! Had more depth to it than I had thought. Good to validate at scale . :)
mergify bot
added a commit
to robfrank/linklift
that referenced
this pull request
Aug 15, 2025
… 2.11.0 to 2.15.0 [skip ci] Bumps [org.openrewrite.recipe:rewrite-static-analysis](https://github.com/openrewrite/rewrite-static-analysis) from 2.11.0 to 2.15.0. Release notes *Sourced from [org.openrewrite.recipe:rewrite-static-analysis's releases](https://github.com/openrewrite/rewrite-static-analysis/releases).* > 2.15.0 > ------ > > What's Changed > -------------- > > * Add parentheses in `CompareEnumsWithEqualityOperator` as needed by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#658](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/658) > * Recipe to simplify Boolean expressions using De Morgan's laws by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#659](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/659) > * SimplifyBooleanExpressionWithDeMorgan - Add parens when converting to OR by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#660](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/660) > * UseForEachLoop recipe by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#651](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/651) > * Do not remove used private fields after type change by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#648](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/648) > * UseForEachLoop - not to change in case of array assignment by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#661](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/661) > * Remove UseForEachLoop by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#662](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/662) > * Fixing `twoCases` test case in `MinimumSwitchCasesTest` by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#672](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/672) > * Fixing code suggestions by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#673](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/673) > * Keep indentation when running `FinalizePrivateFields` by [`@jevanlingen`](https://github.com/jevanlingen) in [openrewrite/rewrite-static-analysis#675](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/675) > * PreferIncrementOperator by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#676](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/676) > * Add MavenJavadocNonAsciiRecipe to remove non-ASCII characters from Ja… by [`@sfarhaazmi`](https://github.com/sfarhaazmi) in [openrewrite/rewrite-static-analysis#674](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/674) > * refactor: Static imports for Collections and Collectors by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#678](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/678) > * PreferIncrementOperator - more coverage - compound assignment operators by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#680](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/680) > * rename settings.local.json to settings.json by [`@zieka`](https://github.com/zieka) in [openrewrite/rewrite-static-analysis#690](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/690) > * Add PreferEqualityComparisonOverDifferenceCheck recipe by [`@e5LA`](https://github.com/e5LA) in [openrewrite/rewrite-static-analysis#686](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/686) > * Applying code suggestions by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#693](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/693) > * Show a failure to remove some semicolons still by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#697](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/697) > * `MoveConditionsToWhile` recipe by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#698](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/698) > * Add Claude derived instructions for tests and recipes by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#700](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/700) > * Fix NPE for new arrays with initializers in `ReplaceCollectionToArrayArgWithEmptyArray` by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#704](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/704) > * Add `RemoveRedundantNullCheckBeforeLiteralEquals` by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#706](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/706) > * Refactor RewriteTest to use defaults method by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#707](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/707) > > New Contributors > ---------------- > > * [`@sfarhaazmi`](https://github.com/sfarhaazmi) made their first contribution in [openrewrite/rewrite-static-analysis#674](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/674) > * [`@zieka`](https://github.com/zieka) made their first contribution in [openrewrite/rewrite-static-analysis#690](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/690) > * [`@e5LA`](https://github.com/e5LA) made their first contribution in [openrewrite/rewrite-static-analysis#686](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/686) > > **Full Changelog**: <openrewrite/rewrite-static-analysis@v2.14.0...v2.15.0> > > 2.14.0 > ------ > > What's Changed > -------------- > > * Heap size of 1G for tests by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#653](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/653) > * UnwrapElseAfterReturn - dealing with trailing comments by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#654](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/654) > * Dealing with even more kinds of trailing comments in UnwrapElseAfterReturn by [`@greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#655](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/655) > > **Full Changelog**: <openrewrite/rewrite-static-analysis@v2.13.0...v2.14.0> > > 2.13.0 > ------ > > What's Changed > -------------- > > * Add CollectionToArrayShouldHaveProperType recipe (RSPEC-S3020) by [`@jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-static-analysis#635](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/635) > * Fix `UnnecessaryCatch` Recipe Multi-Catch Handling by [`@stefanodallapalma`](https://github.com/stefanodallapalma) in [openrewrite/rewrite-static-analysis#638](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/638) > * Add `OnlyCatchDeclaredExceptions` recipe to address RSPEC-S2221 by [`@stefanodallapalma`](https://github.com/stefanodallapalma) in [openrewrite/rewrite-static-analysis#601](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/601) > * RemoveRedundantTypeCast should not remove required downcast by [`@protocol7`](https://github.com/protocol7) in [openrewrite/rewrite-static-analysis#495](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/495) > * Unwrap else after return in preceding `if` by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#645](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/645) > * Extend `InlineVariable` to support local variable assignments by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#647](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/647) ... (truncated) Commits * [`e5ca752`](openrewrite/rewrite-static-analysis@e5ca752) OpenRewrite recipe best practices * [`976d366`](openrewrite/rewrite-static-analysis@976d366) Retain `writeReplace` in `RemoveUnusedPrivateMethods` * [`76322bc`](openrewrite/rewrite-static-analysis@76322bc) Refactor RewriteTest to use defaults method ([#707](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/707)) * [`e5926ef`](openrewrite/rewrite-static-analysis@e5926ef) Add `RemoveRedundantNullCheckBeforeLiteralEquals` ([#706](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/706)) * [`6547c82`](openrewrite/rewrite-static-analysis@6547c82) OpenRewrite recipe best practices * [`c4c5941`](openrewrite/rewrite-static-analysis@c4c5941) Fix NPE for new arrays with initializers in `ReplaceCollectionToArrayArgWithE... * [`428d8b0`](openrewrite/rewrite-static-analysis@428d8b0) Add Claude derived instructions for tests and recipes ([#700](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/700)) * [`e775b10`](openrewrite/rewrite-static-analysis@e775b10) Polish `MoveConditionsToWhile` * [`dbabed5`](openrewrite/rewrite-static-analysis@dbabed5) `MoveConditionsToWhile` recipe ([#698](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/698)) * [`a5a709c`](openrewrite/rewrite-static-analysis@a5a709c) Show a failure to remove some semicolons still ([#697](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/697)) * Additional commits viewable in [compare view](openrewrite/rewrite-static-analysis@v2.11.0...v2.15.0) [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- Dependabot commands and options You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix UnnecessaryCatch Recipe Multi-Catch Handling
Problem
The
UnnecessaryCatchrecipe incorrectly removed entire multi-catch blocks. This resulted in broken code when necessary exception handling was lost.Example of the bug:
Original implementation returns:
Solution
The recipe now filters caught exception types in multi-catch blocks. When only some exceptions are unnecessary, it rewrites the catch clause to include only the necessary types. If all types become unnecessary, the entire catch block is removed.
Before:
After:
Implementation Details
Key Changes
isCheckedExceptionLogic: for enhanced detection of checked exceptions per JLS definitiongetUnnecessaryTypesMethod: Combines checked exception status, thrown exceptions analysis, and configuration optionsReview Focus Areas
isCheckedExceptionlogic: Verify accurate handling of checked exceptionsgetUnnecessaryTypesfiltering: Confirm correct combination of checked exception status, thrown exceptions set, and configuration optionsLimitations
Future Considerations
exceptionListparameter: Consider adding an optionalexceptionListparameter. If provided, the recipe would only target exceptions within this specified list for removal. This could enhance its utility in composite recipes, allowing for more surgical cleanup (e.g., removing only IOException catches during an API migration) without affecting unrelated catch blocks. If this idea is well-received, I am happy to implement it in a future PR.JavaTypeUtils.isCheckedException()utility method for reusability across recipes?Checklist
@timtebeek