Java – PMD: How to ignore only short variable names in lambda expressions

PMD: How to ignore only short variable names in lambda expressions… here is a solution to the problem.

PMD: How to ignore only short variable names in lambda expressions

I’m looking for a solution that allows me to check short variable names using PMD while excluding lambda from that rule.

This is mainly because IntelliJ automatically names common lamba variables with individual characters, in particular. java.lang.String is always abbreviated like this

aListFullOfStrings.forEach(s -> s.replaceAll(" ", ""));

I

prefer this abbreviation, but I don’t want to add suppression to all my lambda expressions. I can’t find any way to tweak my PMD rules to gracefully fix this.

Thank you very much for the advice – I’m not very experienced with all their configurations.

Solution

You can take advantage of xpath-based PMD suppression.

On your ruleset XML, simply configure the properties for that particular rule:

<rule ref="rulesets/java/naming.xml/ShortMethodName">
  <properties>
    <property name="violationSuppressXPath" value=". [ancestor::LambdaExpression]"/>
  </properties>
</rule>

Still, as a PMD

maintainer, I believe this is a legitimate case for changing the rules of PMD itself, so I’ve added github.com/pmd/pmd/issues/720 to track this.

Related Problems and Solutions