How to fix Checkstyle empty block warning

Viewed 3226

I wanted to know the proper way of resolving this issue with a Checkstyle report where it points out an empty else block: "Must have at least one statement". We do not log anything in those classes and I think printing anything on console is also not a good idea. What would be the best way to handle this?

code example with empty "else if" block

2 Answers

You can suppress the warning within a class or block of code by using @SuppressWarnings("checkstyle:EmptyBlock") if you have the SuppressWarningsFilter Checkstyle filter configured.

YourClass.java

public class YourClass {

  @SuppressWarnings("checkstyle:EmptyBlock")
  public void yourMethod() {
    if (someBoolean()) {
      doSomething();
    } else {
      // Do nothing
    }
  }
}

checkstyle.xml

<module name="Checker">
  <module name="SuppressWarningsFilter" />
  ...

  <module name="TreeWalker">
    <module name="SuppressWarningsHolder" />
    ...
  </module>
</module>
Related