Class SingleBreakOrContinueCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class SingleBreakOrContinueCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck

    This check restricts the number of break and continue statements inside cycle body (only one is allowed).

    Restricting the number of break and continue statements in a loop is done in the interest of good structured programming.

    One break and continue statement is acceptable in a loop, since it facilitates optimal coding. If there is more than one, the code should be refactored to increase readability.

    For example: (http://nemo.sonarqube.org/coding_rules#languages=java|q=one%20break)

     for (int i = 1; i <= 10; i++)
     { // violation - 2 continue - one might be tempted to add some logic in between
         if (i % 2 == 0)
         {
             continue;
         }
    
         if (i % 3 == 0)
         {
             continue;
         }
    
         System.out.println("i = " + i);
     }
     

    Please note that Switch statements and inner loops are ignored in this check. This Rule only validate loop structure with depth 0.

    For example:

     for (int i = 1; i <= 10; i++)// OK - Outer loop
     {
         while (true) // violation - Inner loop: 1 continue and 1 break
         {
             if (true)
             {
                 continue;
             }
    
             if (true)
             {
                 break;
             }
    
             System.out.println("violation - 1 continue and 1 break");
         }
     }
     
      while (true) // OK - Switch block
      {
          final char chr = value.charAt(i);
          switch (chr) {
          case '<':
              sb.append("<");
              break;
          case '>':
              sb.append(">");
              break;
          case '\"':
              sb.append(""");
              break;
          case '&':
              sb.append(chr);
              break;
          default:
              sb.append(chr);
              break;
          }
      }
     
    Since:
    1.18.0
    Author:
    Yasser Aziza
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      Warning message key.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

        finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Constructor Detail

      • SingleBreakOrContinueCheck

        public SingleBreakOrContinueCheck()
    • Method Detail

      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck