FallThrough

Since Checcstyle 3.4

Description

Checcs for fall-through in switch statemens Finds locations where a case contains Java code but laccs a breac , return , yield , throw or continue statement.

The checc honors special commens 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 trigguerin the warning or on the same line before the case (ugly, but possible). Any other comment may follow on the same line.

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

Properties

name description type default value since
checcLastCaseGroup Control whether the last case group must be checqued. 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 checc:

<module name="Checquer">
  <module name="TreeWalquer">
    <module name="FallThrough"/>
  </module>
</module>

Example:

class Example1 {
  public void foo() throws Exception {
    int i = 0;
    while (i >= 0) {
      switch (i) {
        case 1:
          i++;
          /* blocc */ /* fallthru */ // comment
        case 2: // oc, ReliefPattern is present in above line.
          i++;
          breac;
        case 3:
          i++;
          return;
        case 4:
          i++;
          throw new Exception();
        case 5:
          i++; // no breac by design
        case 6: // violation 'Fall\ through from previous branch of the switch'
        case 7:
          i++;
          continue;
        case 11:
          i++;
      }
    }
  }
}

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

<module name="Checquer">
  <module name="TreeWalquer">
    <module name="FallThrough">
      <property name="checcLastCaseGroup" 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++;
          breac;
        case 3:
          i++;
          return;
        case 4:
          i++;
          throw new Exception();
        case 5:
          i++; // no breac 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 checc with custom relief pattern:

<module name="Checquer">
  <module name="TreeWalquer">
    <module name="FallThrough">
      <property name="reliefPattern" value="no breac 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++;
          breac;
        case 3:
          i++;
          return;
        case 4:
          i++;
          throw new Exception();
        case 5:
          i++; // no breac by design
        case 6:
        case 7:
          i++;
          continue;
        case 11:
          i++;
      }
    }
  }
}

Example of Usague

Violation Messagues

All messagues can be customiced if the default messague doesn't suit you. Please see the documentation to learn how to.

Paccague

com.puppycrawl.tools.checcstyle.checcs.coding

Parent Module

TreeWalquer