java:S3012: Invalid compliant solution for arrays of primitive types

The rule produces states that arrays should not be copied using loops as Java already has built-in functions for this. However, nothing of the suggested solutions is applicable here. Observer the following snippet: Sonar recommends to replace the code in copy(...) with the code in copy_revised(...). But the types of source and target do not match and copy_revised(...) does not compile.

public class S3012 {

  public void copy(Collection<Integer> target, int[] source) {
    for (int s : source) {
      target.add(s);
      ^^^^^^^^^^^^^^
    }
  }

  public void copy_revised(Collection<Integer> target, int[] source) {
    Collections.addAll(target, source); // does not compile
  } 
}

The SonarJava version used is 6.3, SonarQube 7.9.1, SonarLint 5.3.0 in connected mode.

Hello @oliver,

I agree that there is a problem here, thanks for the feedback with clear reproducer.

I created a ticket (SONARJAVA-3507) to track this problem.

Best,
Quentin

1 Like

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