Skip to content

UseVarForPrimitive emits illegal var address[] = new int[8] for C-style array declarators #1218

Description

@jkschneider

What version of OpenRewrite are you using?

Moderne CLI 4.6.3, rewrite-migrate-java as resolved by org.openrewrite.java.migrate.UpgradeToJava25.

How are you running OpenRewrite?

mod run <org> --recipe <composite including UseVarForPrimitive> over an organization of ten Java repositories.

What is the smallest, simplest way to reproduce the problem?

A local variable declared with a C-style array declarator — brackets after the variable name rather than after the type:

public IeeeAddress deserializeEmberEui64() {
    int address[] = new int[8];
    for (int cnt = 0; cnt < 8; cnt++) {
        address[cnt] = buffer[position++];
    }
    return new IeeeAddress(address);
}

What did you expect to see?

Either the declaration left alone, or rewritten to a legal form:

var address = new int[8];

What did you see instead?

var address[] = new int[8];

which does not compile — var may not be combined with a C-style array declarator (JLS 14.4.1: "It is a compile-time error if ... the declarator has extra array dimension brackets").

What is the full stack trace of any errors you encountered?

No error; the recipe completes and emits invalid source.

Are you interested in contributing a fix to OpenRewrite?

Happy to, if the maintainers agree on the preferred behaviour (skip vs. normalize the declarator).

Additional context

Found while evaluating Java 25 idiom recipes across ten open source repositories. In
zsmartsystems/com.zsmartsystems.zigbee
this fires at 15 sites, e.g.
com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/internal/serializer/EzspDeserializer.java:119.

The C-style form is rare in new code but common in older codebases, which are exactly the ones a Java
version migration runs against. A guard in UseVarForPrimitive that skips declarations whose
J.VariableDeclarations.NamedVariable carries dimensionsAfterName would be the minimal fix;
normalizing the declarator first would be the more useful one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingrecipeRecipe requested

    Type

    No type

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions