java:S3242 false positive using SequencedCollection when order is significant

Make sure to read this post before raising a thread here:

Then tell us:

  • What language is this for? java
  • Which rule? S3242 Use ‘java.util.Collection’ here; it is a more general type than ‘SequencedCollection’.
  • Why do you believe it’s a false-positive/false-negative? code may depend on a predictable order, which cannot be guaranteed by Collection
  • Are you using
    • SonarQube - which version? v10.6 (92116)
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
  // Does not trigger
  public static String joinInOrder(SequencedCollection<String> strings) {
    return String.join(",", strings);
  }

  // Does trigger
  public static String joinInOrderWithStream(SequencedCollection<String> strings) {
    return strings.stream()
        .map(String::trim)
        .collect(Collectors.joining(","));
  }

Hi @throup , welcome to the community, and thanks for reporting!

I could reproduce your example and confirm that we should see it as an issue. While the code still works when replacing a SequencedCollection parameter with Collection (note that the order would still be preserved; that’s a different use-case from instantiation, where such a replacement indeed would introduce a bug), it makes the intent of the developer clear that order is significant, even if no API specific to element order is used.

I created a ticket for tracking the issue here.

Best,
Marco