Skip to content

Commit

Permalink
Don't use var on local array variables that are initialized by an arr…
Browse files Browse the repository at this point in the history
…ay initializer (#552)
  • Loading branch information
MBoegers authored Sep 12, 2024
1 parent 759dbd7 commit 28099cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v
boolean isPrimitive = DeclarationCheck.isPrimitive(vd);
boolean usesGenerics = DeclarationCheck.useGenerics(vd);
boolean usesTernary = DeclarationCheck.initializedByTernary(vd);
if (isPrimitive || usesGenerics || usesTernary) {
boolean usesArrayInitializer = vd.getVariables().get(0).getInitializer() instanceof J.NewArray;
if (isPrimitive || usesGenerics || usesTernary || usesArrayInitializer) {
return vd;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;

import static org.openrewrite.java.Assertions.*;
Expand Down Expand Up @@ -59,6 +60,7 @@ void m() {
);
}


@Test
void reassignment() {
//language=java
Expand Down Expand Up @@ -310,6 +312,25 @@ void m() {

@Nested
class NotApplicable {

@Test
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/551")
void arrayInitializer() {
//language=java
rewriteRun(
java(
"""
package com.example.app;
class A {
void m() {
String[] dictionary = {"aa", "b", "aba", "ba"};
}
}
""")
);
}

@Test
void fieldInAnonymousSubclass() {
//language=java
Expand Down

0 comments on commit 28099cc

Please sign in to comment.