diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariables.java b/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariables.java index 49296fb04..dc08fde4c 100644 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariables.java +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariables.java @@ -43,6 +43,12 @@ public class RemoveUnusedLocalVariables extends Recipe { example = "[unused, notUsed, IGNORE_ME]") String @Nullable [] ignoreVariablesNamed; + @Option(displayName = "Remove unused local variables with side effects in initializer", + description = "Whether to remove unused local variables despite side effects in the initializer. Default false.", + required = false) + @Nullable + Boolean withSideEffects; + @Override public String getDisplayName() { return "Remove unused local variables"; @@ -177,7 +183,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat if (SAFE_GETTER_METHODS.matches(methodInvocation)) { return methodInvocation; } - result.set(true); + if (withSideEffects == null || Boolean.FALSE.equals(withSideEffects)) { + result.set(true); + } return methodInvocation; } diff --git a/src/test/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariablesTest.java b/src/test/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariablesTest.java index 54c3616ff..9aa093a2a 100644 --- a/src/test/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariablesTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariablesTest.java @@ -46,7 +46,7 @@ class RemoveUnusedLocalVariablesTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new RemoveUnusedLocalVariables(new String[0])); + spec.recipe(new RemoveUnusedLocalVariables(new String[0], null)); } @Test @@ -73,18 +73,21 @@ static int method(int x) { @SuppressWarnings("MethodMayBeStatic") void ignoreVariablesNamed() { rewriteRun( - spec -> spec.recipe(new RemoveUnusedLocalVariables(new String[]{"unused", "ignoreMe"})), + spec -> spec.recipe(new RemoveUnusedLocalVariables(new String[]{"unused"}, null)), //language=java java( """ class Test { void method(Object someData) { - int unused = writeDataToTheDB(someData); - int ignoreMe = writeDataToTheDB(someData); + int unused = 123; + int removed = 123; } - - int writeDataToTheDB(Object save) { - return 1; + } + """, + """ + class Test { + void method(Object someData) { + int unused = 123; } } """ @@ -1066,6 +1069,32 @@ static void method() { ); } + @Test + @Issue("https://github.com/openrewrite/rewrite-feature-flags/pull/35") + void removeDespiteSideEffects() { + rewriteRun( + spec -> spec.recipe(new RemoveUnusedLocalVariables(null, true)), + //language=java + java( + """ + class Test { + int sideEffect() { return 123; } + void method(Object someData) { + int unused = sideEffect(); + } + } + """, + """ + class Test { + int sideEffect() { return 123; } + void method(Object someData) { + } + } + """ + ) + ); + } + @Nested class Kotlin {