Skip to content
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 @@ -69,7 +69,8 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
}

// ignores anonymous class fields, contributed code for issue #181
if (this.getCursorToParentScope(this.getCursor()).getValue() instanceof J.NewClass) {
Object parentScope = this.getCursorToParentScope(this.getCursor()).getValue();
if (parentScope instanceof J.NewClass || parentScope instanceof SourceFile) {
return mv;
}

Expand All @@ -88,7 +89,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
}

private Cursor getCursorToParentScope(final Cursor cursor) {
return cursor.dropParentUntil(is -> is instanceof J.NewClass || is instanceof J.ClassDeclaration);
return cursor.dropParentUntil(is -> is instanceof J.NewClass || is instanceof J.ClassDeclaration || is instanceof SourceFile);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.groovy.Assertions.groovy;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.version;
import static org.openrewrite.kotlin.Assertions.kotlin;

@SuppressWarnings("ALL")
class FinalizeLocalVariablesTest implements RewriteTest {
Expand Down Expand Up @@ -484,6 +486,30 @@ public void doSomething() {
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/702")
@Test
void topLevelKotlinVar() {
rewriteRun(
kotlin(
"""
var x = 1
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/702")
@Test
void topLevelGroovyVariable() {
rewriteRun(
groovy(
"""
x = 1
"""
)
);
}

@Test
void initializedInTryCatchFinally() {
rewriteRun(
Expand Down
Loading