Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-999

"goto" should jump to labels declared later in the same function

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      This goto statement must be replaced by a standard iteration statement.
    • Default Severity:
      Blocker
    • Impact:
      High
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Legacy Key:
      BackJumpWithGoto, BackwardsGoto
    • Covered Languages:
      C, C++, Objective-C, PL/SQL
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      1h
    • PC-Lint:
      9064

      Description

      Unconstrained use of goto can lead to programs that are extremely difficult to comprehend and analyse. For C++, it can also lead to the program exhibiting unspecified behavior.

      However, in many cases a total ban on goto requires the introduction of flags to ensure correct control flow, and it is possible that these flags may themselves be less transparent than the goto they replace.

      Therefore, the restricted use of goto is allowed where that use will not lead to semantics contrary to developer expectations. "Back" jumps are prohibited, since they can be used to create iterations without using the well-defined iteration statements supplied by the core language.

      Noncompliant Code Example

      int f() {
        int j = 0;
      L1:
        ++j;
        if (10 == j) {
          goto L2;         // forward jump ignored
        }
        // ...
        goto L1;           // Noncompliant
      L2:
        return ++j;
      }
      

      Compliant Solution

      int f() {
        for (int j = 0; j < 11; j++) {
          // ...
        }
        return ++j;
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                Anonymous
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated: