java:S6206 False positive - suggesting to use record removing restriction on constructor visibility

Version information:

  • SonarLint (for JetBrains IDEs)

In the following sample, the type has public access modifier while the constructor has the default package-private one. The constructor resembles the records’ canonical constructor except for its more restrictive access modifier.

public final class Foo {

    private final int bar;

    Foo(int bar) { = bar;

Sonar recommends converting this class to a record. However, records require their constructors to have the same or less restrictive modifier than that of the record. If this class is converted to a record then it will be impossible to restrict creation of new Foo objects.

I suggest that the rule takes into consideration access modifier of the constructor before it suggests to use a Record.

Hi @szw-dariusz, I created SONARJAVA-4090 to improve the rule. Thanks for your feedback. Alban

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