openrewrite / rewrite-migrate-java

OpenRewrite recipes for migrating to newer versions of Java.
Apache License 2.0
92 stars 64 forks source link

Rewrite of empty `Set.of()` by UseEnumSetOf doesn't compile #490

Open olayinkasf opened 1 month ago

olayinkasf commented 1 month ago

What version of OpenRewrite are you using?

I am using

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project.

<plugin>
  <groupId>org.openrewrite.maven</groupId>
  <artifactId>rewrite-maven-plugin</artifactId>
  <version>5.32.1</version>
  <configuration>
    ... 
  </configuration>
</plugin>

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

class A {
    void foo(String bar) {
        final Set<TimeUnit> timeUnits = Set.of();
    }
}

What did you expect to see?

Either use EnumSet.noneOf or do not do any rewrite

class A {
    void foo(String bar) {
        final Set<TimeUnit> timeUnits = EnumSet.noneOf(TimeUnit.class);
    }
}

What did you see instead?

Uses EnumSet.of which requires an argument and since there is none, the code below does not compile

class A {
    void foo(String bar) {
        final Set<TimeUnit> timeUnits = EnumSet.of();
    }
}

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

No exception from plugin. Rewritten code isn't compilable.

Are you interested in contributing a fix to OpenRewrite?

I can fix it when I have time but I don't know when I will so please feel free to do that.

timtebeek commented 1 month ago

Oh wow, thanks for reporting the issues you're finding! Seems like a straightforward special case replacement to get this fixed.