- Operating system: Windows 10
- SonarLint plugin version: 7.1.1.54565
- Programming language you’re coding in: Java
- Is connected mode used: No
Hello,
I am dealing with a problem regarding rule java:S6207.
I’ve been able to reduce it so it triggers on the following code
public record SonarBug(int arg1, int arg2) {
public SonarBug(int arg1, int arg2) { // noncompliant
this.arg1 = arg1;
if (arg2 == 5) {
arg2 = 4;
}
this.arg2 = arg2;
}
}
The SonarLint plugin marks the constructor to be “redundant”, most probably because it detects that its arguments are directly assigned to the fields of the record. However, the 2nd argument is reassigned before this happens.
The following modification is considered to be compliant.
public record SonarBug(int arg1, int arg2) {
public SonarBug(int arg1, int arg2) { // compliant
this.arg1 = arg1;
if (arg2 == 5) {
this.arg2 = 4;
} else {
this.arg2 = arg2;
}
}
}
However, I feel like the first version of the constructor should also be compliant.
Moreover, a ternary operator with the same logic is also compliant.
public record SonarBug(int arg1, int arg2) {
public SonarBug(int arg1, int arg2) { // compliant
this.arg1 = arg1;
this.arg2 = arg2 == 5 ? 4 : 5;
}
}