We are using CheckStyle to enforce our style standards. One of the style rules we opted to include was the NeedBraces module.
NeedBraces specifies that every block type statement (such as if
, else
, for
) must have opening and closing curly braces. However, as far as I can tell it isn't working entirely correctly.
This example will trigger a CheckStyle error.
if (true)
{
System.out.println("20");
}
else
System.out.println("30");
Because the else case doesn't have braces. However, the next example fails to trigger a CheckStyle error.
if (true)
{
System.out.println("20");
}
else
if (true)
{
System.out.println("30");
}
This should have failed because of the missing braces on the else case, but checkstyle lets it pass. After double checking the documentation, I can't find any reason why this isn't working right.
So... Can the CheckStyle module "NeedBraces" work with nested if/else blocks? Any ideas?
The answer to this question begs another question: is there a rule to flag the above undesirable code as a violation?