Checkstyle: problem with regexing spaces in spaces

I am adding Checkstyle to my project, but the rule for detecting spaces is not enough (RegexpSingleline):

(?<=\S)\s+$ 

It detects trailing spaces and ignores only lines with spaces (it must allow nested empty lines). It works fine fine, but it complains about javadoc / multi-line comments using an empty line:

 /** * Some text * * More text */ 

The empty line between the two lines is "asterisk-whitespace" (the default formatting is Eclipse), thereby invoking this rule, and so far I have not been able to force it to ignore this special situation. How can I fix this regex for this case?

Thanks!

Note: there is no need to be multi-line check and check whether the line is really part of the comment, it is thin enough as a separate line.


Summing up the desired rules ...

The regular expression should match lines with trailing spaces:

 x = y; void function() { 

Except in cases where there is nothing in the line except spaces (in this exception, only one asterisk is allowed up to the last space, but ONLY when the asterisk is the only non-space char):

 (only whitespaces here, all ok) /** * (this text is not here, and this line is ok - this case is my uncovered situation) */ 
+6
source share
4 answers

Sorry, I forgot about this issue.

In the end, my last regular expression was as follows:

 (?<!\A[ \t]*\*?)[ \t]$ 
0
source

Replace the original expression

 \s+$ 

to

 (?<!\*)\s+$|\*\s\s+$ 

The left side of the pipe looks for spaces at the beginning of the line if it starts with *. The right side of the pipe looks for double spaces at the end of the line.

Edit:

In the meantime, the Eclipse code formatter fixed a problem with trailing spaces in empty JavaDoc lines. Hopefully we no longer have to configure the Checkstyle regex here;)

+4
source

How about this:

 (?<=\S)(?<!^\s*\*)\s+$ 

matches a space at the end of a line only if it is preceded by a character without spaces, which is also not the only asterisk in the line.

This regular expression requires variable-length lookbehind statements that most regular expression systems do not support. If this does not work, you need to match the whole line:

 ^(?!\s*(?:\*\s+)?$)(.*?)\s+$ 

and replace the matches with \1 to remove the spaces at the end of the line.

0
source

Our (older than 5.7) version of Checkstyle uses an XML file to declare rules and rejects the '<' in the xml value attribute (lookbehind token) found in the extended rule using @crasp. I came up with this as an alternative solution to ignore trailing spaces in line comments, block comments and empty lines. Our junior developers cannot regularly use the Eclipse formatter, and they are the usual suspects for trailing spaces. This rule instructs Checkstyle to ignore most of this garbage:

 ^(?!(?:[ \t]*?(?:\/?\*\*?|\/\/)[\S \t]+?|[ \t]+$))[\S \t]+[ \t]+$ 
0
source

Source: https://habr.com/ru/post/907406/


All Articles