SonarLint quick fix for java:S1612 is invalid Java due to name clashes

  • Operating system: NixOS
  • SonarLint plugin version: 3.16.0
  • Programming language you’re coding in: Java
  • Is connected mode used: No
    • Connected to SonarCloud or SonarQube (and which version): No

The following code:

import java.util.function.UnaryOperator;

class $ {
    public $ $;

    public $ id() {
        return this;
    }

    public static UnaryOperator<$> $Op = ($ it) -> it.id();
}

is erroneously corrected to this:

import java.util.function.UnaryOperator;

class $ {
    public $ $;

    public $ id() {
        return this;
    }

    public static UnaryOperator<$> $Op = $::id;
}

which produces the error:

non-static variable $ cannot be referenced from a static context

Hi Anselm,

welcome to the community!

Please update your SonarLint plugin version, it seems that the problem you reported was already fixed. I tested with SinarLint 8.0.0 and it does handle that corner case correctly:

In case the quick fix would cause a name clash, the full-qualified class name is used:

public static UnaryOperator<$> $Op = ($ it) -> it.id(); // Noncompliant

Quick fix:

public static UnaryOperator<$> $Op = org.example.$::id; // Compliant

In case the class is located in the default package (i.e., no package declaration), SonarLint does not report S1612.

Best,
Marco

1 Like

Thank you

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