
org.openrewrite.java.cleanup.RemoveUnusedLocalVariablesTest.kt Maven / Gradle / Ivy
/*
* Copyright 2021 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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.java.cleanup
import org.junit.jupiter.api.Test
import org.openrewrite.Issue
import org.openrewrite.Recipe
import org.openrewrite.java.JavaRecipeTest
@Suppress(
"ConstantConditions",
"StatementWithEmptyBody",
"EmptyTryBlock",
"CatchMayIgnoreException",
"UnusedAssignment",
"ResultOfMethodCallIgnored",
"BooleanMethodNameMustStartWithQuestion",
"PointlessBooleanExpression",
"UseOfObsoleteCollectionType",
"UnnecessaryLocalVariable",
"EmptyFinallyBlock",
"ClassInitializerMayBeStatic"
)
interface RemoveUnusedLocalVariablesTest : JavaRecipeTest {
override val recipe: Recipe
get() = RemoveUnusedLocalVariables()
@Test
@Issue("https://github.com/openrewrite/rewrite/issues/841")
fun ignoreSuppressWarnings() = assertUnchanged(
before = """
class Test {
static int method(int x) {
int a = 0;
@SuppressWarnings("unused") int b = 0;
return a + 1;
}
}
"""
)
@Test
fun removeUnusedLocalVariables() = assertChanged(
before = """
class Test {
static int method(int x) {
int a = 0;
int b = 0;
return a;
}
}
""",
after = """
class Test {
static int method(int x) {
int a = 0;
return a;
}
}
"""
)
@Test
fun removeUnusedLocalVariablesReassignedButNeverUsed() = assertChanged(
before = """
class Test {
static int method() {
int isRead = -1;
int notRead = 0;
notRead = 1;
notRead += 1;
notRead = isRead + 1;
return isRead + 1;
}
}
""",
after = """
class Test {
static int method() {
int isRead = -1;
return isRead + 1;
}
}
"""
)
@Test
fun ignoreClassFields() = assertUnchanged(
before = """
class Test {
int a = 0;
int b = 1;
int method(int x) {
b = 2;
return x + 1;
}
}
"""
)
@Test
fun removeUnusedLocalVariablesInClassInitializer() = assertChanged(
before = """
class Test {
static {
int unused = 0;
int used = 1;
System.out.println(used);
}
{
int unused = 0;
int used = 1;
System.out.println(used);
}
}
""",
after = """
class Test {
static {
int used = 1;
System.out.println(used);
}
{
int used = 1;
System.out.println(used);
}
}
"""
)
@Test
fun handleLocalVariablesShadowingClassFields() = assertChanged(
before = """
class Test {
int a = 0;
int unused = 1;
static int method(int x) {
int unused = 2;
return x + 1;
}
}
""",
after = """
class Test {
int a = 0;
int unused = 1;
static int method(int x) {
return x + 1;
}
}
"""
)
@Test
fun localVariableUnusedIncrementOperation() = assertChanged(
before = """
class Test {
static boolean isTrue() {
return false;
}
static int method(int x) {
int a = 0;
int b = 99;
a++;
for (int i = 0; isTrue(); i++) {
a++;
}
return b++;
}
}
""",
after = """
class Test {
static boolean isTrue() {
return false;
}
static int method(int x) {
int b = 99;
for (int i = 0; isTrue(); i++) {
}
return b++;
}
}
"""
)
@Test
@Issue("https://github.com/apache/dubbo/blob/747282cdf851c144af562d3f92e10349cc315e36/dubbo-metadata/dubbo-metadata-definition-protobuf/src/test/java/org/apache/dubbo/metadata/definition/protobuf/model/GooglePB.java#L938-L944")
fun keepLocalVariablesAssignmentOperationToOtherVariables() = assertUnchanged(
before = """
class Test {
static int method() {
int dataSize = 0;
int size = 0;
for (int j = 0; j < 10; j++) {
dataSize += 1;
}
size += dataSize;
return size;
}
}
"""
)
@Test
@Issue("https://github.com/openrewrite/rewrite/blob/706a172ed5449214a4a08637a27dbe768fb4eecd/rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java#L55-L65")
fun keepLocalVariableAssignmentOperation() = assertUnchanged(
before = """
class Test {
static boolean method() {
boolean a = false;
return a |= false;
}
}
"""
)
@Test
@Issue("https://github.com/openrewrite/rewrite/blob/706a172ed5449214a4a08637a27dbe768fb4eecd/rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java#L55-L65")
fun removeUnusedLocalVariableBitwiseAssignmentOperation() = assertChanged(
before = """
class Test {
static boolean method() {
boolean a = false;
boolean b = false;
b &= true;
return a |= false;
}
}
""",
after = """
class Test {
static boolean method() {
boolean a = false;
return a |= false;
}
}
"""
)
@Test
@Issue("https://github.com/openrewrite/rewrite/blob/706a172ed5449214a4a08637a27dbe768fb4eecd/rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java#L55-L65")
fun keepLocalVariableBitwiseAssignmentOperationWithinExpression() = assertUnchanged(
before = """
class Test {
static boolean method(String string) {
boolean a = false;
for (char c : string.toCharArray()) {
if (false || (a |= !Character.isWhitespace(c))) {
break;
}
}
return a;
}
}
"""
)
@Test
@Issue("https://github.com/openrewrite/rewrite/blob/706a172ed5449214a4a08637a27dbe768fb4eecd/rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java#L55-L65")
fun handleInstanceOf() = assertUnchanged(
before = """
import java.util.Stack;
class Test {
static boolean method(Stack