java:S6207 - Possible false positive

  • 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;
    }
}

Hello @Deathcofi,
This indeed looks like a false positive. This is due to a limitation in the implementation that checks that the expected parameter is assigned to the expected component. In your first sample, the arg2 component receives the value of the arg2 parameter but because you chose to modify the value of the parameter this can no longer be considered a redundant assignment.

A ticket has been created to fix the issue.