Skip to content

Commit 0ef25b2

Browse files
committed
Added RULE 15-4
1 parent e3c2298 commit 0ef25b2

4 files changed

Lines changed: 68 additions & 7 deletions

File tree

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
/**
22
* @id c/misra/loop-iteration-condition
33
* @name RULE-15-4: There should be no more than one break or goto statement used to terminate any iteration statement
4-
* @description
4+
* @description More than one break or goto statement in iteration conditions may lead to
5+
* readability and maintainability issues.
56
* @kind problem
67
* @precision very-high
78
* @problem.severity error
89
* @tags external/misra/id/rule-15-4
10+
* maintainability
11+
* readability
912
* external/misra/obligation/advisory
1013
*/
1114

1215
import cpp
1316
import codingstandards.c.misra
1417

15-
from
18+
from Loop loop
1619
where
17-
not isExcluded(x, Statements2Package::loopIterationConditionQuery()) and
18-
select
20+
not isExcluded(loop, Statements2Package::loopIterationConditionQuery()) and
21+
count(Stmt terminationStmt |
22+
loop.getChildStmt*() = terminationStmt and
23+
(
24+
terminationStmt instanceof BreakStmt
25+
or
26+
terminationStmt instanceof GotoStmt
27+
)
28+
) > 1
29+
select loop, "$@ statement contains more than one break or goto statement", loop, "Iteration"
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
No expected results have yet been specified
1+
| test.c:24:3:32:3 | for(...;...;...) ... | $@ statement contains more than one break or goto statement | test.c:24:3:32:3 | for(...;...;...) ... | Iteration |
2+
| test.c:38:3:45:3 | while (...) ... | $@ statement contains more than one break or goto statement | test.c:38:3:45:3 | while (...) ... | Iteration |
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
void f1() {
2+
L1:;
3+
4+
for (int k = 0; k < 10; k++) { // COMPLIANT
5+
;
6+
}
7+
8+
for (int i = 0; i < 10; i++) { // COMPLIANT
9+
if (i > 5) {
10+
break;
11+
}
12+
}
13+
14+
for (int j = 0; j < 10; j++) { // COMPLIANT
15+
goto L1;
16+
}
17+
}
18+
19+
void f2() {
20+
L1:;
21+
22+
int k = 0;
23+
24+
for (int i = 0; i < 10; i++) { // NON_COMPLIANT
25+
if (i > 5) {
26+
break;
27+
}
28+
if (i < 10) {
29+
break;
30+
}
31+
goto L1;
32+
}
33+
34+
while (k < 10) { // COMPLIANT
35+
;
36+
}
37+
38+
while (k < 10) { // NON_COMPLIANT
39+
if (k > 5) {
40+
break;
41+
}
42+
while (k < 3) { // COMPLIANT
43+
goto L1;
44+
}
45+
}
46+
}

rule_packages/c/Statements2.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@
4747
},
4848
"queries": [
4949
{
50-
"description": "",
50+
"description": "More than one break or goto statement in iteration conditions may lead to readability and maintainability issues.",
5151
"kind": "problem",
5252
"name": "There should be no more than one break or goto statement used to terminate any iteration statement",
5353
"precision": "very-high",
5454
"severity": "error",
5555
"short_name": "LoopIterationCondition",
56-
"tags": []
56+
"tags": [
57+
"maintainability",
58+
"readability"
59+
]
5760
}
5861
],
5962
"title": "There should be no more than one break or goto statement used to terminate any iteration statement"

0 commit comments

Comments
 (0)