[Java:S1170] Ignore, if initialization accesses instance members

Rule S1170 should not raise an issue, if initialization accesses instance members.

public abstract class C
{
    private final C c = this; // FP
    private final X x = getX(); // FP
    private final String s = "abcdef"; // Correctly raise an issue!
    
    protected abstract X getX();
}

Hi @mfroehlich,

Thank you for reporting this false positive. I totally agree that we should fix it. Attributes can’t be static if their initialization references instance members.
Here is the ticket I created: SONARJAVA-3617

Cheers,
Nicolas

Great, thanks.

While I agree that we should not report an issue for such cases, I can only reproduce a FP for private final C c = this; but not for the second private final X x = getX();.

Am I missing something?

You are right. I misread the issue I got on private final X x = getX();. No False Positive there. I’ll update the ticket.

@mfroehlich Are you able to reproduce the second issue with the latest version of SonarQube/SonarLint?

Tbh, that getX() thing was not from my code. I just thought, I could complete the example. So, please forget about it.

private final C c = this; // FP

This is, what was causing the FP actually.

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