Skip to content

Change default or IllegalIdentifierName #15499

@romani

Description

@romani

https://checkstyle.org/checks/naming/illegalidentifiername.html#IllegalIdentifierName

Checks identifiers with a pattern for a set of illegal names, such as those that are restricted or contextual keywords.

from #15105 and #15486 (comment)

JLS Keywords as of Java 22:
https://docs.oracle.com/javase/specs/jls/se22/html/jls-3.html#jls-3.9


$ cat TestClass.java
public class Test {
    public void test() {
       String record = "some data";        // violation
    }
}

PS> C:\Programs\jdk-22.0.1\bin\javac.exe .\TestClass.java
PS>

$ cat TestConfig.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
    <module name="TreeWalker">
       <module name="IllegalIdentifierName"/>
    </module>
</module>

$ java -jar checkstyle-10.17.0-all.jar -c TestConfig.xml TestClass.java
Starting audit...
[WARN] TestClass.java:3:5: 
Name 'record' must match pattern '(?i)^(?!(record|yield|var|permits|sealed|_)$).+$'.[IllegalIdentifierName]
Audit done.

I see no problem with such name record, IDEs are also ok
image
this name is ok and word is frequent (not rare).

I pretty sure such code is fully fine:

boolean sealed = isSealed();

Check make sense to forbid var var = 1 or record record(record r){} , attention to fact that record is not good as name for RECORD_DEF, var is not good value for variable name.

This Check suppose to not violate names in certain context.

I agree to fully and for all forbid var I think for most of targets, all other is mostly for type names.
So default will forbid var and xxxx$ as names.

_ as name will be well common, as in jdk21 has unnamed variables. Let's not validate it, by default.

in Examples we should show examples how to add more JLS Keywords if users wants and he is confident that in his projects there should be no usage of words that are same as Keywords of Java.

/var/tmp$ java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

/var/tmp$ cat Test.java 
class Test {
	int x$;
	int var;
}
/var/tmp$ javac Test.java 
/var/tmp$ 

so let by default not allow 'var' and any name that has $ in name as it highly recommended to not use as it used in system generated code only.

I hope /^(?!(var)$)[a-zA-Z_]+$ is what we need as new default property value.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions