Skip to content

Commit 88be969

Browse files
committed
Added RULE-14-3
1 parent 3071732 commit 88be969

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @id c/misra/controlling-exp-invariant-condition
3+
* @name RULE-14-3: Controlling expressions shall not be invariant
4+
* @description If a controlling expression has an invariant value then it is possible that there is
5+
* a programming error.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-14-3
10+
* correctness
11+
* maintainability
12+
* readability
13+
* external/misra/obligation/required
14+
*/
15+
16+
import cpp
17+
import codingstandards.c.misra
18+
19+
from ControlFlowNode expr, string message
20+
where
21+
not isExcluded(expr, Statements5Package::controllingExpInvariantConditionQuery()) and
22+
(
23+
exists(IfStmt ifStmt |
24+
(
25+
ifStmt.getControllingExpr() = expr and
26+
(
27+
conditionAlwaysFalse(expr)
28+
or
29+
conditionAlwaysTrue(expr)
30+
)
31+
)
32+
) and
33+
message = "Controlling expression in if statement has invariant value."
34+
)
35+
or
36+
exists(Loop loop |
37+
loop.getControllingExpr() = expr and
38+
(
39+
conditionAlwaysFalse(expr)
40+
or
41+
conditionAlwaysTrue(expr)
42+
)
43+
) and
44+
message = "Controlling expression in loop statement has invariant value."
45+
or
46+
exists(SwitchStmt switch |
47+
switch.getControllingExpr() = expr and
48+
(
49+
conditionAlwaysFalse(expr) and
50+
conditionAlwaysTrue(expr)
51+
)
52+
) and
53+
message = "Controlling expression in switch statement has invariant value."
54+
select expr, message
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:2:7:2:11 | ... > ... | Controlling expression in if statement has invariant value. |
2+
| test.c:13:10:13:16 | ... > ... | Controlling expression in loop statement has invariant value. |
3+
| test.c:14:9:14:13 | ... > ... | Controlling expression in if statement has invariant value. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-14-3/ControllingExpInvariantCondition.ql
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
void f1(int p1) {
2+
if (2 > 3) { // NON_COMPLIANT
3+
}
4+
5+
if (p1 > 0) { // COMPLIANT
6+
}
7+
8+
if (p1 < 10 && p1 > 20) { // NON_COMPLIANT[FALSE_NEGATIVE]
9+
}
10+
}
11+
12+
void f2(int p1) {
13+
while (20 > 10) { // NON_COMPLIANT
14+
if (1 > 2) {
15+
} // NON_COMPLIANT
16+
}
17+
}

0 commit comments

Comments
 (0)