Add RemoveUnreachableMultiCatchAlternative#926
Merged
Conversation
Removes `catch` alternatives that are already covered by an earlier `catch` clause in the same `try`. When a multi-catch reduces to a single alternative it collapses to a regular `catch`; when all alternatives are shadowed the entire `catch` clause is dropped. This pattern most commonly appears after type-substitution migrations that cause two `catch` clauses to overlap (for example, renaming an exception so its parent and child end up in adjacent catches), which is a Java compile error. Intended to run before `CombineSemanticallyEqualCatchBlocks` so that collapsed singletons become candidates for combination.
Contributor
|
When the recipe drops an entire |
greg-at-moderne
approved these changes
Jun 30, 2026
Call maybeRemoveImport for shadowed types that are removed, and add a test covering specific catches retained before a trailing catch-all Exception block.
timtebeek
approved these changes
Jun 30, 2026
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.
Summary
Adds
RemoveUnreachableMultiCatchAlternative, which removescatchalternatives that are already covered by an earliercatchclause in the sametry:catch (X) {} catch (X | Y) {}→catch (X) {} catch (Y) {}catch (X) {} catch (Y | X) {}→catch (X) {} catch (Y) {}catch (IOException) {} catch (FileNotFoundException | IllegalStateException) {}→catch (IOException) {} catch (IllegalStateException) {}(subtype shadowed)catch (IOException) {} catch (FileNotFoundException | EOFException) {}→catch (IOException) {}(whole later catch dropped)When a multi-catch reduces to a single alternative it collapses back to a regular
catch; when all alternatives are shadowed the entirecatchclause is removed.Motivation
The pattern shows up after type-substitution migrations that cause two
catchclauses to overlap — for example, when a recipe renames an exception so its parent and child end up in adjacent catches. The result is a Java compile error (exception X has already been caught). This recipe cleans that up by dropping the unreachable alternative.Intended to run before
CombineSemanticallyEqualCatchBlocksso collapsed singletons become candidates for combination.Notes
NoMissingTypes— won't fire when type attribution is incomplete.Test plan
dropAlternativeShadowedByEarlierCatch—catch (X) ... catch (X | Y)(@DocumentExample)dropTrailingAlternativeShadowedByEarlierCatch—catch (X) ... catch (Y | X)dropMiddleAlternativeShadowedByEarlierCatch—catch (X) ... catch (Y | X | Z)dropSubtypeAlternativeShadowedByEarlierCatch— alternative is a subtypedropEntireCatchWhenAllAlternativesAreShadoweddropEntireSingleTypeCatchWhenShadoweddoNothingWhenOrderingMeansNothingIsShadoweddoNothingWhenMultiCatchAlternativesAreAllNeeded