From 9deef9a01fa1166b06a5d6bd686b23900349ef71 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 23 Oct 2024 12:02:50 +0200 Subject: [PATCH] Allow removal of unused local variables with side effects in initializer For https://github.com/openrewrite/rewrite-feature-flags/pull/35 --- .../RemoveUnusedLocalVariables.java | 10 ++++- .../RemoveUnusedLocalVariablesTest.java | 43 ++++++++++++++++--- 2 files changed, 45 insertions(+), 8 deletions(-) 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 {