Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseEnumSetOf rewrite breaks for varargs parameter #516

Closed
mbruggmann opened this issue Jul 26, 2024 · 2 comments · Fixed by #518
Closed

UseEnumSetOf rewrite breaks for varargs parameter #516

mbruggmann opened this issue Jul 26, 2024 · 2 comments · Fixed by #518
Assignees
Labels
bug Something isn't working good first issue Good for newcomers test provided

Comments

@mbruggmann
Copy link
Contributor

mbruggmann commented Jul 26, 2024

What version of OpenRewrite are you using?

rewrite-migrate-java 2.20.0

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

I can reproduce the issue with the following testcase:

  @Test
  void enumSetOfVarargs() {
    rewriteRun(
        spec -> spec.recipe(new UseEnumSetOf()),
        java(
            """
            package com.helloworld;

            import java.util.concurrent.TimeUnit;
            import java.util.Set;

            public class Main {

              public Set<TimeUnit> method(final TimeUnit... units) {
                final Set<TimeUnit> asSet = Set.of(units);
                return asSet;
              }

            }"""));
  }
}

What did you expect to see?

Either to bail out and keep it as is.

Or alternatively we could translate the varargs parameter into a collection first, e.g.

public Set<TimeUnit> method(final TimeUnit... units) {
  final Set<TimeUnit> asSet = EnumSet.noneOf(TimeUnit.class);
  asSet.addAll(Arrays.asList(units));
  return asSet;
}

Note that the more convenient-looking EnumSet.copyOf(asList(units)) throws an exception for empty array.

What did you see instead?

public Set<TimeUnit> method(final TimeUnit... units) {
  final Set<TimeUnit> asSet = EnumSet.of(units);
  return asSet;
}

(doesn't compile)

@mbruggmann mbruggmann added the bug Something isn't working label Jul 26, 2024
@timtebeek
Copy link
Contributor

Thanks for the report @mbruggmann ! Bail and return the original LST element is likely the quickest safe fix when there's a potentially empty array as argument. Is this something you'd be open to take on?

@mbruggmann
Copy link
Contributor Author

@timtebeek Seemed simple enough so I gave it a go: #518

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers test provided
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants