Skip to content

Commit d89c8cb

Browse files
committed
added RULE-14-2
1 parent 755fc46 commit d89c8cb

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @id c/misra/for-loop-not-well-formed
3+
* @name RULE-14-2: A for loop shall be well-formed
4+
* @description A well-formed for loop makes code easier to review.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity recommendation
8+
* @tags external/misra/id/rule-14-2
9+
* readability
10+
* maintainability
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.Loops
17+
18+
from ForStmt for
19+
where
20+
not isExcluded(for, Statements4Package::forLoopNotWellFormedQuery()) and
21+
isInvalidLoop(for)
22+
select for, "For loop is not well formed."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No expected results have yet been specified
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-14-2/ForLoopNotWellFormed.ql
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
#include "stdbool.h"
3+
int g1 = 10;
4+
int f1() { return g1++; }
5+
6+
void f2() {
7+
for (float f = 0.0F; f < 10.0F; f += 0.2F) { // NON_COMPLIANT
8+
}
9+
for (int i = 0; i < 10; i++) { // COMPLIANT
10+
}
11+
}
12+
13+
void f3() {
14+
for (int i = 0, j = 0; i < j; i++, j++) { // NON_COMPLIANT
15+
}
16+
}
17+
18+
void f4() {
19+
int i, j;
20+
for (i = 0, j = 0; i < j; i++, j++) { // NON_COMPLIANT
21+
}
22+
}
23+
24+
void f5() {
25+
for (int i = 0; i != 10; i += 3) { // NON_COMPLIANT
26+
}
27+
28+
for (int i = 0; i != 10; i++) { // COMPLIANT
29+
}
30+
}
31+
32+
void f7() {
33+
for (int i = 0; i < 100; i += g1) { // COMPLIANT
34+
}
35+
}
36+
37+
void f8() {
38+
for (int x = 0; x < 5; x += f1()) { // NON_COMPLIANT
39+
}
40+
}
41+
42+
void f9() {
43+
bool l1 = true;
44+
for (int x = 0; (x < 5) && l1; ++x) { // COMPLIANT
45+
l1 = false;
46+
}
47+
}
48+
49+
bool f10(int p1) { return false; }
50+
void f11() {
51+
bool p1 = true;
52+
for (int x = 0; (x < 5); p1 = f10(++x)) { // NON_COMPLIANT
53+
}
54+
}
55+
56+
void f12() {
57+
bool l1 = true;
58+
for (int x = 0; (x < 5) && l1; ++x) { // COMPLIANT
59+
}
60+
}
61+
62+
void f13() {
63+
int l1 = 1;
64+
for (int x = 0; x < 5 && l1 == 9; ++x) { // NON_COMPLIANT
65+
x = x + 2;
66+
g1--;
67+
}
68+
}

0 commit comments

Comments
 (0)