java:S1452 Collectors should be exempt from this rule for the second generic parameter

Using SonarLint IntelliJ

The following code returns java:S1452. I believe this shouldn’t be reported, as in Collector, the accumulator type is often hidden as an implementation detail.

Sample code (yes, I know it’s pretty much Collectors.summingInt, but this applies to all collectors).


class Scratch {
    public static Collector<Integer, ?, Integer> summing() {
        return Collector.of(
                () -> new int[1],
                (a, t) -> a[0] += t,
                (a, b) -> {
                    a[0] += b[0];
                    return a;
                a -> a[0]
1 Like

Hello @xfix

I agree with you, even the documentation states that it is often hidden as an implementation detail.

Usually, for specific cases, I would advise you to resolve the issue as “won’t fix”.

In this case, since it’s coming from java.util, I believe it deserves an exception in the rule. I created a ticket (SONARJAVA-3357 ) to track this issue.

Thanks for raising the point.


1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.