False Positive for Squid:3958 (Intermediate Stream methods should not be left unused)

900 views
Skip to first unread message

Paul Wagland

unread,
Sep 8, 2017, 7:58:09 PM9/8/17
to SonarQube
Hi all,

As you have heard from me before, I love using SonarQube, and the additional rules that are being added only adds more value to an already valuable product!

We have been looking at Squid:3958, and have discovered a couple of false positive situations.

The first:

  public static void falsePositive1() {

    List<Boolean> list = new ArrayList<>();

    list.stream().filter(b -> !b.booleanValue()).sequential().collect(Collectors.toList());

    list.stream().filter(b -> !b.booleanValue()).collect(Collectors.toList());

  }


The underlined section is marked as unused intermediate stream methods. This is clearly nonsensical, as sequential() is called. Removing the call to sequential() causes the warning to go away. This can be seen in the second line, as there is no highlighting.


The second false positive:

  private static boolean a(Iterable<Boolean> iterable) { return true; }


  public static boolean falsePositive2() {

    List<Boolean> list = new ArrayList<>();

    Stream<Boolean> filter = StreamSupport.stream(list.spliterator(), false).filter(Boolean::booleanValue);

    return a(filter::iterator);

  }



Again, you can see the underlined section is where the warning is raised, however it is always used, as it is passed to a() as a lambda which uses the iterator(), and iterator() is a terminal operation. Note that calling the terminal operation directly is fine:

  public static boolean ok2() {

    List<Boolean> list = new ArrayList<>();

    Stream<Boolean> filter = StreamSupport.stream(list.spliterator(), false).filter(Boolean::booleanValue);

    filter.iterator();

    return true;

  }



It only seems to fail when passed as method reference to another method/class.

Cheers,
Paul

Michael Gumowski

unread,
Sep 19, 2017, 5:25:30 AM9/19/17
to Paul Wagland, SonarQube
Hey Paul,

Thanks for your feedback and precise, self-contained, reproducers. I created the following tickets to handle these cases independently: 
Cheers,
Michael


--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/aca1bbda-40b0-42f8-a065-edd91129d901%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Michael Gumowski | SonarSource
Software Developer, Language Team
http://sonarsource.com

Paul Wagland

unread,
Sep 21, 2017, 8:34:28 AM9/21/17
to SonarQube


On Tuesday, September 19, 2017 at 11:25:30 AM UTC+2, Michael Gumowski wrote:
Hey Paul,

Thanks for your feedback and precise, self-contained, reproducers. I created the following tickets to handle these cases independently: 

You are welcome, and, as always, thanks to you and your colleagues for a great product!

Cheers,
Paul
 
Reply all
Reply to author
Forward
0 new messages