Version information:
- SonarLint 6.3.0.39716 (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) {
this.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.