views:

668

answers:

2

Based on this question it appears that the default template for CheckStyle will allow if else ladders to separate the if and else with a line break.

Meaning I would like this code to be flagged as a violation:

if (true)
{
    System.out.println("20");   
}
else
    if (true)
    {
        System.out.println("30");
    }

Is there a CheckStyle rule to prevent this? Looking over the docs, I don't see one, and I'd prefer not to use the generic regex rule, if I don't have to.

Also, if I use the GenericIllegalRegexp module, multiline regex don't seem to work. Is there some remedy to this?

+1  A: 

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.

VonC
Course if you use + instead of * as the quantifier, it will ignore poorly formed code which doesn't have extra spaces on both lines.
Justin Standard
Update. This doesn't seem to work, as checkstyle appears to ignore regular expressions that rely on the MULTILINE flag, even if you use the embedded flag in your expression.
Justin Standard
I do confirm the regexp is correctly detected in multi-line configuration in Checkstyle: it does not require any (?m) multiline flag to work
VonC
Could this be an issue with CheckStyle version? (I'm on the 5.0beta) Using the regex you provided doesn't flag the code in my example. (BTW, I agree wiht your version of the regex, if only it worked. It looks like it should, but I get no style error)
Justin Standard
Also, I'll add that I can change the regex used to a single line match and it works, so I'm strongly convinced that there is something working funny related to multiline matches.
Justin Standard
You are right: Do not use Generic Illegal Regexp, but rather "Regexp". See my answer I just completed
VonC
That does it. Thank you much for all the help. Looks to me like CheckStyle questions don't get much traffic...
Justin Standard
A: 

IntelliJ lets you set this kind of behavior as a global default. If everyone on your team used IntelliJ, you could have a team style guideline and the IDE would enforce it.

Or you can use CheckStyle if you aren't an IntelliJ shop.

duffymo