Skip to content

Leave a Groovy def declaration alone instead of splicing var into its variable name. - #1229

Open
motlin wants to merge 1 commit into
openrewrite:mainfrom
motlin:groovy-def-var-splice
Open

Leave a Groovy def declaration alone instead of splicing var into its variable name.#1229
motlin wants to merge 1 commit into
openrewrite:mainfrom
motlin:groovy-def-var-splice

Conversation

@motlin

@motlin motlin commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

What's changed?

DeclarationCheck.isSingleVariableDefinition now refuses declarations that expose no type expression for var to replace. The single guard covers UseVarForConstructors, UseVarForObject, UseVarForGenericsConstructors, and UseVarForGenericMethodInvocations.

What's your motivation?

The var recipes corrupt Groovy def declarations, non-idempotently.

// before
def newFile = new File(file.parentFile, newName)
assert file.renameTo(newFile) : "Failed to rename ${file} to ${newFile}"

// after one run
def varnewFile = new File(file.parentFile, newName)
assert file.renameTo(newFile) : "Failed to rename ${file} to ${newFile}"

// after a second run on the same file
def varvarnewFile = new File(file.parentFile, newName)

Originally hit on rewrite-migrate-java 3.42.1 with rewrite-maven-plugin 6.46.1, against a real
Maven archetype post-generate script. Still reproduces on main at v3.43.0.

Anything in particular you'd like reviewers to focus on?

Have you considered any alternatives or workarounds?

Applying an org.openrewrite.FindSourceFiles precondition of **/*.java to the var
recipes.

@motlin

motlin commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

See also liftwizard/liftwizard#4125

@motlin
motlin force-pushed the groovy-def-var-splice branch from 03573b5 to 19833ec Compare September 1, 2026 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant