FallThrough

Since Checkstyle 3.4

Description

Checks for fall-through in switch statements. Finds locations where a case contains Java code but lacks a break, return, yield, throw or continue statement.

The check honors special comments to suppress the warning. By default, the texts "fallthru", "fall thru", "fall-thru", "fallthrough", "fall through", "fall-through" "fallsthrough", "falls through", "falls-through" (case-sensitive). The comment containing these words must be all on one line, and must be on the last non-empty line before the case triggering the warning or on the same line before the case (ugly, but possible).

Note: The check assumes that there is no unreachable code in the case.

Properties

name description type default value since
checkLastCaseGroup Control whether the last case group must be checked. boolean false 4.0
reliefPattern Define the RegExp to match the relief comment that suppresses the warning about a fall through. Pattern "falls?[ -]?thr(u|ough)" 4.0

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="FallThrough"/>
  </module>
</module>
        

Example:

class Example1 {
  public void foo() throws Exception {
    int i = 0;
    while (i >= 0) {
      switch (i) {
        case 1:
          i++;
        case 2: // violation 'Fall through from previous branch of the switch'
          i++;
          break;
        case 3:
          i++;
          return;
        case 4:
          i++;
          throw new Exception();
        case 5:
          i++; // no break by design
        case 6: // violation 'Fall through from previous branch of the switch'
        case 7:
          i++;
          continue;
        case 11:
          i++;
      }
    }
  }
}
        

To configure the check to enable check for last case group:

<module name="Checker">
  <module name="TreeWalker">
    <module name="FallThrough">
      <property name="checkLastCaseGroup" value="true"/>
    </module>
  </module>
</module>
        

Example:

class Example2 {
  public void foo() throws Exception {
    int i = 0;
    while (i >= 0) {
      switch (i) {
        case 1:
          i++;
        case 2: // violation 'Fall through from previous branch of the switch'
          i++;
          break;
        case 3:
          i++;
          return;
        case 4:
          i++;
          throw new Exception();
        case 5:
          i++; // no break by design
        case 6: // violation 'Fall through from previous branch of the switch'
        case 7:
          i++;
          continue;
        case 11: // violation 'Fall through from the last branch of the switch'
          i++;
      }
    }
  }
}
        

To configure the check with custom relief pattern:

<module name="Checker">
  <module name="TreeWalker">
    <module name="FallThrough">
      <property name="reliefPattern" value="no break by design"/>
    </module>
  </module>
</module>
        

Example:

class Example3 {
  public void foo() throws Exception {
    int i = 0;
    while (i >= 0) {
      switch (i) {
        case 1:
          i++;
        case 2: // violation 'Fall through from previous branch of the switch'
          i++;
          break;
        case 3:
          i++;
          return;
        case 4:
          i++;
          throw new Exception();
        case 5:
          i++; // no break by design
        case 6:
        case 7:
          i++;
          continue;
        case 11:
          i++;
      }
    }
  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.coding

Parent Module

TreeWalker