SonarLint: S6212 Local-Variable Type Inference should be used on final object?

  • versions used: SonarLint
  • error observed: final Object inner = new Object(); causes “code-smells” on syntactic sugar with the suggestion to adjust this to final var inner = new Object();.
  • potential workaround: manually opt-out of this rule. Apart from the strange suggestion our pre-JDK11 code is now littered with noise of “code-smells” for this rule cloaking other issues that we would like to solve. This is the first time in the many years that I’ve been using and advocating the use of SonarLint pre-commit as a second pair of eyes that I’ve had to disable a default rule. If a rule is arbitrary I would expect it to be opt-in, not opt-out so the developers themselves can decide to enforce it. Even in this is stated: “While it is not always possible or cleaner to use this new way of declaring a variable”. Please leave this up to the developers themselves.
1 Like

Seconded. This is an optional feature of the language and as such, should be optional in the ruleset, rather than default in Sonar Way and therefore impossible to disable without resorting to manual database intervention.

As a further comment, even the JEP and it’s associated documents suggest that this notation is not always desireable:

From Local Variable Type Inference: Style Guidelines -

"There is a certain amount of controversy over this feature. Some welcome the concision it enables; others fear that it deprives readers of important type information, impairing readability. And both groups are right. "


" Like all features, it must be used with judgment. There’s no blanket rule for when it should and shouldn’t be used."


@jhutting I’m not sure to understand your point, technically, without speaking about personal taste, is there something wrong with replacing final Object inner = new Object(); by final var inner = new Object();?

@Tony.Noble After discussions, we indeed decided to remove it from the default quality profile. More details: SONARJAVA-3870.

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