LambdaParameterName

Since Checkstyle 8.11

Description

Checks lambda parameter names.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[a-z][a-zA-Z0-9]*$" 8.11

Examples

To configure the check:

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

Code Example:

Function<String, String> function1 = s -> s.toLowerCase(); // OK
Function<String, String> function2 = S -> S.toLowerCase(); // violation, name 'S'
                                               // must match pattern '^[a-z][a-zA-Z0-9]*$'
        

An example of how to configure the check for names that begin with a lower case letter, followed by letters is:

<module name="Checker">
  <module name="TreeWalker">
    <module name="LambdaParameterName">
      <property name="format" value="^[a-z]([a-zA-Z]+)*$"/>
    </module>
  </module>
</module>
        

Code Example:

class MyClass {
  Function<String, String> function1 = str -> str.toUpperCase().trim(); // OK
  Function<String, String> function2 = _s -> _s.trim(); // violation, name '_s'
                                             // must match pattern '^[a-z]([a-zA-Z]+)*$'

  public boolean myMethod(String sentence) {
    return Stream.of(sentence.split(" "))
            .map(word -> word.trim()) // OK
            .anyMatch(Word -> "in".equals(Word)); // violation, name 'Word'
                                   // must match pattern '^[a-z]([a-zA-Z]+)*$'
  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.naming

Parent Module

TreeWalker