java:S1319 FP using EnumSet instead of the Set interface

  • What language is this for? Java
  • Which rule? S1319: The type should be an interface such as “Set” rather than the implementation “EnumSet”
  • Why do you believe it’s a false-positive/false-negative? EnumSet has some specialised methods, which do not exist on any interface.
  • Are you using
    • SonarQube Server / Community Build - which version? v10.6 (92116)
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
public enum Fruit {
  APPLE,
  BANANA,
  ORANGE,
  PEAR;

  // triggers here
  public static final EnumSet<Fruit> TASTY_FRUIT = EnumSet.of(APPLE, ORANGE, PEAR);
}

// -- in another class
public class AnotherClass {
  public static Stream<Fruit> nastyFruitStream() {
    // not possible with just a Set<Fruit> because
    // enum-specific logic is required to find the compliment
    return EnumSet.complementOf(Fruit.TASTY_FRUIT).stream();
  }
}

Hi Chris, thanks for the report.

Indeed, the rule suggestion would break the code in this case.
I created a ticket to work around the issue.

Have a nice day!