Skip to content

Google style checks should allow _ keyword for anonymous variables #18283

@eamonnmcmanus

Description

@eamonnmcmanus

I have read check documentation: https://checkstyle.org/checks/xxxxxx/nameofaffectedcheck.html
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

/var/tmp $ javac YOUR_FILE.java

/var/tmp $ cat config.xml
[Using standard `google_checks.xml`]

/var/tmp $ cat YOUR_FILE.java
package blim;

import java.util.HashMap;

public class Blim {
  record Point(int x, int y) {}
  
  void blim() {
    var _ = new Thread();
    switch ("foo") {
      case String _ -> System.out.println("bar");
    }
    switch (new Point(0, 0)) {
      case Point(int _, _) -> System.out.println("point");
    }
    try {
      // something
    } catch (Error _) {
      // something else
    }
    for (var _ : new String[] {"foo"}) {}
    var map = new HashMap<String, Integer>();
    map.computeIfAbsent("foo", _ -> 23);
  }
}

/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-12.2.0-all.jar -c config.xml YOUR_FILE.java
Starting audit...
[WARN] /private/tmp/Blim.java:5:1: Missing a Javadoc comment. [MissingJavadocType]
[WARN] /private/tmp/Blim.java:9:9: Local variable name '_' must match pattern '^[a-z]([a-z0-9][a-zA-Z0-9]*)?$'. [LocalVariableName]
[WARN] /private/tmp/Blim.java:11:19: Pattern variable name '_' must match pattern '^[a-z]([a-z0-9][a-zA-Z0-9]*)?$'. [PatternVariableName]
[WARN] /private/tmp/Blim.java:14:22: Pattern variable name '_' must match pattern '^[a-z]([a-z0-9][a-zA-Z0-9]*)?$'. [PatternVariableName]
[WARN] /private/tmp/Blim.java:18:20: Catch parameter name '_' must match pattern '^[a-z]([a-z0-9][a-zA-Z0-9]*)?$'. [CatchParameterName]
[WARN] /private/tmp/Blim.java:21:14: Local variable name '_' must match pattern '^[a-z]([a-z0-9][a-zA-Z0-9]*)?$'. [LocalVariableName]
[WARN] /private/tmp/Blim.java:23:32: Lambda parameter name '_' must match pattern '^[a-z]([a-z0-9][a-zA-Z0-9]*)?$'. [LambdaParameterName]
Audit done.

I would not expect any warnings here (except the javadoc one).

Quoting what I wrote on the related #16768:

Hi, I'm one of the maintainers of Google's Java style guide (cited above). The situation is that this guide is a copy of Google's internal style guide. Internally, we do not yet support a version of Java that allows _ in place of an identifier. We are probably not going to update the guide to mention _ explicitly until we do. However, I would argue that even as written, the guide does not preclude _ lambda parameters. Sections 5.1 and 5.2 are respectively "Rules common to all identifiers" and "Rules by identifier type", but _ is not an identifier, it is a keyword. So the guidelines in 5.1 and 5.2 don't apply to it.

When Google does internally support _, we will update the guide to say that _ is allowed everywhere it is legal. That change will probably be in Section 5.1.

So I think it would make sense to update the rules without waiting for an explicit change to the guide. I believe it would just involve updating all of LambdaParameterName, CatchParameterName, LocalVariableName, and PatternVariableName so that the regex changes from ^[a-z]([a-z0-9][a-zA-Z0-9]*)?$ to ^(_|[a-z]([a-z0-9][a-zA-Z0-9]*)?)$.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions