SonarQube C#
  1. SonarQube C#
  2. SONARCS-263

New rule: 'break' should not be used outside of 'switch'

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.1
    • Labels:
      None
    • Number of attachments :
      0

      Description

      Title

      'break' should not be used outside of 'switch'

      Description

      <p>
      The <code>break;</code> statement, as its name indicates, breaks the control flow of the program in an unstructured way.
      It reduces the program readability, and makes refactoring more complex when used outside of <code>switch</code>.
      </p>
      
      <p>The following code:</p>
      
      <pre>
      int i = 0;
      while (true)
      {
        if (i == 10)
        {
          break;      // Non-Compliant
        }
      
        Console.WriteLine(i);
        i++;
      }
      </pre>
      
      <p>should be refactored into:</p>
      
      <pre>
      int i = 0;
      while (i != 10) // Compliant
      {
        Console.WriteLine(i);
        i++;
      }
      </pre>
      
      <p>The following code is compliant:</p>
      
      <pre>
      int foo = 0;
      switch (foo)
      {
        case 0:
          Console.WriteLine("foo = 0");
          break;      // Compliant
      }
      </pre>
      

      Message

      Refactor the code in order to remove this break statement.

      Severity

      Major

      In Sonar way?

      Yes

        Activity

        Hide
        Dinesh Bolkensteyn added a comment -

        In fact, the results look so good that I believe the rule should be enabled by default.

        Show
        Dinesh Bolkensteyn added a comment - In fact, the results look so good that I believe the rule should be enabled by default.
        Hide
        Dinesh Bolkensteyn added a comment -

        Should be fixed

        Show
        Dinesh Bolkensteyn added a comment - Should be fixed
        Hide
        Fabrice Bellingard added a comment -

        Perfect!

        Show
        Fabrice Bellingard added a comment - Perfect!

          People

          • Assignee:
            Dinesh Bolkensteyn
            Reporter:
            Fabrice Bellingard
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: