Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new check RecordComponentName #8765

Closed
nrmancuso opened this issue Aug 27, 2020 · 2 comments · Fixed by #9180
Closed

Create new check RecordComponentName #8765

nrmancuso opened this issue Aug 27, 2020 · 2 comments · Fixed by #9180

Comments

@nrmancuso
Copy link
Member

From #8764:

➜  src /usr/lib/jvm/java-14-openjdk/bin/javac --enable-preview --source 14 TestClass.java                               
Note: TestClass.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
➜  src cat config.xml                                                                                                   
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
    <property name="charset" value="UTF-8"/>
    <module name="TreeWalker">
        <module name="LocalVariableName">
            <property name="format" value="(?i)^(?!(record|yield|var)$).+$"/>
        </module>
        <module name="TypeName">
            <property name="format" value="(?i)^(?!(record|yield|var)$).+$"/>
        </module>
    </module>
</module>%                                                                                                                     ➜  src cat TestClass.java                                                                                               
public class TestClass {
    public static void main(String... args) {
        var var = 4; // violation, "var" should not be used as an identifier.

        int record = 15; // violation, "record" should not be used as an identifier.

        String yield = "yield"; // violation, "yield" should not be used as an identifier.

        record Record // violation, "Record" should not be used as an idenitifier.
                (Record record) { // should be violation, "record" should not be used as an identifier.
        }

        String yieldString = "yieldString"; // ok, part of another word
        record MyRecord(){} // ok, part of another word
        var variable = 2; // ok, part of another word
    }
}
➜  src java -jar /home/nick/IdeaProjects/checkstyle/target/checkstyle-8.36-SNAPSHOT-all.jar -c config.xml TestClass.java\

Starting audit...
[ERROR] /home/nick/Desktop/full-record-grammar/check-updates/src/TestClass.java:3:13: Name 'var' must match pattern '(?i)^(?!(record|yield|var)$).+$'. [LocalVariableName]
[ERROR] /home/nick/Desktop/full-record-grammar/check-updates/src/TestClass.java:5:13: Name 'record' must match pattern '(?i)^(?!(record|yield|var)$).+$'. [LocalVariableName]
[ERROR] /home/nick/Desktop/full-record-grammar/check-updates/src/TestClass.java:7:16: Name 'yield' must match pattern '(?i)^(?!(record|yield|var)$).+$'. [LocalVariableName]
[ERROR] /home/nick/Desktop/full-record-grammar/check-updates/src/TestClass.java:9:16: Name 'Record' must match pattern '(?i)^(?!(record|yield|var)$).+$'. [TypeName]
Audit done.
Checkstyle ends with 4 errors.

I would expect there to be a check that allows users to enfore naming conventions on record components; there should be an ability to specify a format property, similar to MemberNameCheck (after all, record components are essentially member variables). Simply adding support for record components to MemberNameCheck is not a good solution, since there are many other properties in MemberNameCheck that do not apply here.

Do we need another new check, RecordComponentName?

We have LambdaParameterName, MemberName, and ParameterName. Since record components are basically "members", we should make it possible for users to enforce naming conventions on them.

@pbludov
Copy link
Member

pbludov commented Aug 27, 2020

The record components are both fields and parameters. If the naming policy for fields and parameters differs, we have an inconsistency for record component names. A dedicated check will solve this issue.

@romani romani added this to To do in Java 14 syntax features via automation Aug 27, 2020
@romani romani removed this from To do in Java 14 syntax features Oct 31, 2020
strkkk added a commit to strkkk/checkstyle that referenced this issue Jan 13, 2021
strkkk added a commit to strkkk/checkstyle that referenced this issue Jan 13, 2021
strkkk added a commit to strkkk/checkstyle that referenced this issue Jan 18, 2021
strkkk added a commit to strkkk/checkstyle that referenced this issue Jan 19, 2021
@rnveach rnveach added this to the 8.40 milestone Jan 24, 2021
@rnveach
Copy link
Member

rnveach commented Jan 24, 2021

Fixed merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants