-
Notifications
You must be signed in to change notification settings - Fork 92
Simplify ifs with literal conditions even if they jump
#193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ac68464
Get rid of dead code after returns or throws
tkindy aefc6de
Operate on all blocks
tkindy e5a39d9
Handle break and continue
tkindy e214168
Fix style
tkindy c0e148d
Add failing test for return in else block
tkindy 887a0cb
Check for jumps in branch we're keeping
tkindy 6dac178
Improve tests
tkindy a12efec
Prefix J subclasses
tkindy 6af4d27
Fix style
tkindy 990c386
Do nothing if jump at end of block
tkindy 55aab50
Add nested test
tkindy 4f1c1ea
Add license header
tkindy 3d754c2
Add blockless tests
tkindy 2d00a8f
Remove unnecessary Lombok annotation
tkindy 06dc801
Remove jump check
tkindy 716d226
Update RemoveUnreachableCodeVisitor.java
tkindy dcd0eb9
Merge branch 'main' into tk/simplify-if-dead-code
timtebeek f826037
Add test with try/catch/throws
pstreef File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/main/java/org/openrewrite/staticanalysis/RemoveUnreachableCodeVisitor.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright 2023 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.staticanalysis; | ||
|
|
||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.internal.ListUtils; | ||
| import org.openrewrite.java.JavaVisitor; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.Statement; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| class RemoveUnreachableCodeVisitor extends JavaVisitor<ExecutionContext> { | ||
|
|
||
| @Override | ||
| public J visitBlock(J.Block block, ExecutionContext executionContext) { | ||
| block = (J.Block) super.visitBlock(block, executionContext); | ||
|
|
||
| List<Statement> statements = block.getStatements(); | ||
| Optional<Integer> maybeFirstJumpIndex = findFirstJump(statements); | ||
| if (!maybeFirstJumpIndex.isPresent()) { | ||
| return block; | ||
| } | ||
| int firstJumpIndex = maybeFirstJumpIndex.get(); | ||
|
|
||
| if (firstJumpIndex == statements.size() - 1) { | ||
| // Jump is at the end of the block, so nothing to do | ||
| return block; | ||
| } | ||
|
|
||
| List<Statement> newStatements = | ||
| ListUtils.flatMap( | ||
| block.getStatements(), | ||
| (index, statement) -> { | ||
| if (index <= firstJumpIndex) { | ||
| return statement; | ||
| } | ||
| return Collections.emptyList(); | ||
| } | ||
| ); | ||
|
|
||
| return block.withStatements(newStatements); | ||
| } | ||
|
|
||
| private Optional<Integer> findFirstJump(List<Statement> statements) { | ||
| for (int i = 0; i < statements.size(); i++) { | ||
| Statement statement = statements.get(i); | ||
| if ( | ||
| statement instanceof J.Return || | ||
| statement instanceof J.Throw || | ||
| statement instanceof J.Break || | ||
| statement instanceof J.Continue | ||
| ) { | ||
| return Optional.of(i); | ||
| } | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.