I am not sure you can easily write a Checkstyle extension, since the AST browsing code of the Checkstyle SDK Gui does not make any difference between:
else if
and
else
if
In each case, else
is a LITERAL_ELSE
with in it an if
...
So the generic regexp else[ \t]*[\r\n]+[ \t]*if
is indeed a quick way to detect that kind of code.
Fix the regexp to include cases were :
- there is no space not tab before/after newline
- the is multiple newlines.
Of course, you do not want to use the \s
whitespace regexp expression, since it includes itself newline characters. It is clearer to separate spaces from return characters.
Note: in Checkstyle 5.0beta, do not use "Generic Illegal Regexp", but rather the "Regexp" module:
you can configure that RegExp module as 'illegalPattern' (with an associated Severity of 'Warning'), and you do not have to use any kind af 'multi-line' flag: the regexp is enough.
Regexp
Description
A check that makes sure that a specified pattern exists, exists less than a set number of times, or does not exist in the file.
This check combines all the functionality provided by RegexpHeader, GenericIllegalRegexp and RequiredRegexp, except supplying the regular expression from a file.
It differs from them in that it works in multiline mode. It's regular expression can span multiple lines and it checks this against the whole file at once. The others work in singleline mode. Their single or multiple regular expressions can only span one line. They check each of these against each line in the file in turn.