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

Loops with at most one iteration should be refactored

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
      None
    • Message:
      Refactor this loop to do more than one iteration.
    • Highlighting:
      Hide
      • primary: first line of the loop statement
      • secondary: all the nested control flow statements return, break, throw or goto leading to have at most one iteration
        message: 'The loop stops here.'
      Show
      primary: first line of the loop statement secondary: all the nested control flow statements return , break , throw or goto leading to have at most one iteration message: 'The loop stops here.'
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Cobol, Flex, PL/I, RPG, VB6
    • Covered Languages:
      ABAP, C#, C, C++, Go, Java, JavaScript, Objective-C, PHP, PL/SQL, Python, Swift, T-SQL, TypeScript, VB.Net
    • Irrelevant for Languages:
      HTML, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Level:
      Control-flow Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • ESLint-SonarJS:
      no-one-iteration-loop
    • PMD:
      AvoidBranchingStatementAsLastInLoop
    • PVS-Studio:
      V612
    • TSLint-SonarTS:
      no-unconditional-jump
    • VisualStudio:
      CS0162

      Description

      A loop with at most one iteration is equivalent to the use of an if statement to conditionally execute one piece of code. No developer expects to find such a use of a loop statement. If the initial intention of the author was really to conditionally execute one piece of code, an if statement should be used instead.

      At worst that was not the initial intention of the author and so the body of the loop should be fixed to use the nested return, break or throw statements in a more appropriate way.

      Noncompliant Code Example

      for (int i = 0; i < 10; i++) { // noncompliant, loop only executes once
        printf("i is %d", i);
        break;
      }
      ...
      for (int i = 0; i < 10; i++) { // noncompliant, loop only executes once
        if (i == x) {
          break;
        } else {
          printf("i is %d", i);
          return;
        }
      }
      

      Compliant Solution

      for (int i = 0; i < 10; i++) {
        printf("i is %d", i);
      }
      ...
      for (int i = 0; i < 10; i++) {
        if (i == x) {
          break;
        } else {
          printf("i is %d", i);
        }
      }
      

        Attachments

          Issue Links

          1.
          Swift RSPEC-2940 Language-Specification Active Unassigned
          2.
          JavaScript RSPEC-3813 Language-Specification Active Unassigned
          3.
          C# RSPEC-3980 Language-Specification Active Unassigned
          4.
          VB.Net RSPEC-4247 Language-Specification Active Unassigned
          5.
          ABAP RSPEC-4250 Language-Specification Active Unassigned
          6.
          PL/SQL RSPEC-4254 Language-Specification Active Unassigned
          7.
          T-SQL RSPEC-4506 Language-Specification Active Unassigned
          8.
          Go RSPEC-4611 Language-Specification Active Unassigned
          9.
          PHP RSPEC-4952 Language-Specification Active Unassigned
          10.
          Python RSPEC-5480 Language-Specification Active Unassigned
          11.
          C-Family RSPEC-5518 Language-Specification Active Unassigned

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                ann.campbell.2 Ann Campbell
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: