java:S1190 complains about unnamed variables in java 21 with preview features enabled

  • Language: Java
  • Rule: java:S1190 “Future keywords should not be used as names”
  • Running SonarLint SonarLint 10.1.0.81817 under eclipse 2024-03 (4.31)

Java 21 introduced unnamed variables (“_”) as a preview feature (JEP 443; see also JEP 456 for java 22), yet the rule complains about using them:

public static void method(final int[] array) {
   	try {
		java.util.Arrays.setAll(array, _ -> 0); // Warning here, at the underscore
	} catch(NullPointerException _) { /* ... */ } // Another warning here
}

The rule should only warn about using _ as a variable name in java versions 8 and lower: between versions 9 and 20 it’s an error to use it at all, in version 21 it’s only okay if preview features are enabled, and starting from version 22 it’s always(-ish) okay.

See also Java 21 - unamed pattern error

1 Like