-
Type:
Bug Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:None
-
Message:Refactor this loop to do more than one iteration.
-
Highlighting:
-
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
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); } }
- is duplicated by
-
RSPEC-3554 Loop stop conditions should allow more than one iteration
- Closed
- is implemented by
-
SONARPY-809 S1751: Add a message on the secondary location
-
- Open
-
-
SONARCOBOL-1418 Rule S1751: Loops with at most one iteration should be refactored
-
- Open
-
-
SONARFLEX-119 Rule: Loops with at most one iteration should be refactored
-
- Open
-
-
SONARPLI-176 Rule: Loops with at most one iteration should be refactored
-
- Open
-
-
SONARRPG-173 Rule: Loops with at most one iteration should be refactored
-
- Open
-
-
SONARVBSIX-256 Rule: Loops with at most one iteration should be refactored
-
- Open
-
-
CPP-617 Rule: Jump statements should not be used unconditionally
-
- Closed
-
-
SONARABAP-312 Rule: Jump statements should not be used unconditionally
-
- Closed
-
-
SONARJAVA-512 Rule S1751: Jump statements should not be used unconditionally
-
- Closed
-
-
SONARJS-822 Rule "Jump statements should not be used unconditionally"
-
- Closed
-
-
SONARPHP-666 Rule S1751: Loops with at most one iteration should be refactored
-
- Closed
-
-
SONARPLSQL-610 Rule: Jump statements should not be used unconditionally
-
- Closed
-
-
SONARPY-207 Rule: Loops with at most one iteration should be refactored
-
- Closed
-
-
SONARPY-442 Rule S1751: Loops with at most one iteration should be refactored
-
- Closed
-
-
SONARTSQL-110 Rule: Loops with at most one iteration should be refactored
-
- Closed
-
-
SONARVB-198 Rule S1751: Jump statements should not be used unconditionally
-
- Closed
-
-
CPP-707 Create Objective-C rules repository containing targeted rules
-
- Closed
-
-
SONARPLSQL-754 S1751: Add a message on the secondary location
-
- Closed
-
-
SONARSWIFT-114 Rule: Jump statements should not be used unconditionally
-
- Closed
-
-
SONARSWIFT-480 S1751: Add a message on the secondary location
-
- Closed
-
- is related to
-
CPP-2797 S1751, S1763: deduplicate, prefer specific S1751 over generic S1763
-
- Open
-
-
SONARABAP-364 Adjust S1751: exclude redundant jumps
-
- Closed
-
-
MMF-764 Java: Rules from FindBugs & PMD
-
- Closed
-
-
RSPEC-1763 All code should be reachable
- Active
- relates to
-
SONARPHP-855 FP S1751 : for loop without update
-
- Closed
-
-
RSPEC-3626 Jump statements should not be redundant
- Active
- links to
1.
|
Swift | RSPEC-2940 |
|
Active | Unassigned | |
2.
|
JavaScript | RSPEC-3813 |
|
Active | Unassigned | |
3.
|
C# | RSPEC-3980 |
|
Active | Unassigned | |
4.
|
VB.Net | RSPEC-4247 |
|
Active | Unassigned | |
5.
|
ABAP | RSPEC-4250 |
|
Active | Unassigned | |
6.
|
PL/SQL | RSPEC-4254 |
|
Active | Unassigned | |
7.
|
T-SQL | RSPEC-4506 |
|
Active | Unassigned | |
8.
|
Go | RSPEC-4611 |
|
Active | Unassigned | |
9.
|
PHP | RSPEC-4952 |
|
Active | Unassigned | |
10.
|
Python | RSPEC-5480 |
|
Active | Unassigned | |
11.
|
C-Family | RSPEC-5518 |
|
Active | Unassigned |