1

I have a situation where Checkstyle is producing false positives on the ParenPad rule check. I also saw this SO article Checkstyle: Usage of ParenPad Check and this question is unrelated.

Consider the following code segment:

public class Foo

    public static final void main(      String... args ) { ... }

}

When ParenPad is configured like so:

<module name="ParenPad">
    <property name="option" value="space" />
</module>

This result will show as valid since there is a space before the opening ( in the main method.

I was poking around in the ParenPad check class and I didn't see any other rules/configurations that could be used to enforce a single space before and/or after a parenthesis. Is there something built-in that allows me to enforce this requirement or do I need to create a custom check? From what I can tell, the current implementation checks for the existence for any number of spaces be it tab delimited or otherwise.

Community
  • 1
  • 1
djthoms
  • 3,026
  • 2
  • 31
  • 56

1 Answers1

0

Looking at the checkstyle code, the only way I see currently is by adding another check. First you define the ParenPad and then you add two RegexpSinglelines

<module name="RegexpSingleline">
    <property name="format" value="(\s{2,}$"/>
    <property name="message" value="Too many spaces after ("/>
</module>
<module name="RegexpSingleline">
    <property name="format" value="\s{2,})$"/>
    <property name="message" value="Too many spaces before )"/>
</module>

True, it can break in other cases (e.g. a string that has a ( or )), but you can extend it to cover majority of your cases.

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115