Skip to content
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

RemoveUnusedLocalVariables fails inside case blocks (test) #153

Merged
merged 5 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
parent instanceof J.MethodDeclaration ||
// skip if defined in an enhanced or standard for loop, since there isn't much we can do about the semantics at that point
parent instanceof J.ForLoop.Control || parent instanceof J.ForEachLoop.Control ||
// skip if defined in a switch case
parent instanceof J.Case ||
// skip if defined in a try's catch clause as an Exception variable declaration
parent instanceof J.Try.Resource || parent instanceof J.Try.Catch || parent instanceof J.MultiCatch ||
// skip if defined as a parameter to a lambda expression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,7 @@ fun foo() {
@Test
void retainJavaUnusedLocalVariableWithNewClass() {
rewriteRun(
//language=java
java(
"""
class A {}
Expand All @@ -1030,6 +1031,41 @@ void foo() {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/152")
void retainUnusedInsideCase() {
rewriteRun(
//language=java
java(
"""
class Test {
static void method() {
int x = 10;
char y = 20;
switch (x) {
case 10:
byte unused;
break;
}
}
}
""",
"""
class Test {
static void method() {
int x = 10;
switch (x) {
case 10:
byte unused;
break;
}
}
}
"""
)
);
}

@Nested
class Kotlin {

Expand All @@ -1053,12 +1089,14 @@ fun foo() {
@ExpectedToFail("Not yet implemented")
void retainUnusedLocalVariableConst() {
rewriteRun(
//language=kotlin
kotlin(
"""
package constants
const val FOO = "bar"
"""
),
//language=kotlin
kotlin(
"""
package config
Expand Down