Pattern: Use of empty block
Issue: -
Empty block statements, while not technically errors, usually occur due to refactoring that wasn’t completed. They can cause confusion when reading code.
By default, this rule disallows empty blocks for if
, else
, switch
, try
and finally
statements. It ignores block statements which contain a comment (for example, in an finally block of try statement to indicate that execution should continue regardless of errors).
<module name="EmptyBlock">
<property name="option" value="TEXT"/>
<property name="tokens" value="LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/>
</module>
Example of incorrect code:
if(optimistic)
{
}
else
{
}
Example of correct code:
if(optimistic)
{
message = "half full";
}
else
{
message = "half empty";
}