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.