Skip to content

Commit ddc2892

Browse files
committed
WIP 16-7
1 parent 297f339 commit ddc2892

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
/**
22
* @id c/misra/switch-expression-bool-condition
33
* @name RULE-16-7: A switch-expression shall not have essentially Boolean type
4-
* @description
4+
* @description An `if-else` construct is more appropriate for boolean controlled expression.
55
* @kind problem
66
* @precision very-high
77
* @problem.severity error
88
* @tags external/misra/id/rule-16-7
9+
* readability
10+
* maintainability
911
* external/misra/obligation/required
1012
*/
1113

1214
import cpp
1315
import codingstandards.c.misra
16+
import codingstandards.cpp.SwitchStatement
1417

15-
from
16-
where
17-
not isExcluded(x, Statements2Package::switchExpressionBoolConditionQuery()) and
18-
select
18+
from BooleanSwitchStmt boolSwitch
19+
where not isExcluded(boolSwitch, Statements2Package::switchExpressionBoolConditionQuery())
20+
select boolSwitch, "Boolean expression used in switch."
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
No expected results have yet been specified
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
void f1(int p1) {
2+
3+
switch (p1) // COMPLIANT
4+
{
5+
case 1:;
6+
break;
7+
case 2:;
8+
break;
9+
default:
10+
break;
11+
}
12+
}
13+
14+
void f2(int p1) {
15+
switch (p1 == 1) // NON_COMPLIANT
16+
{
17+
case 0:
18+
break;
19+
case 1:
20+
break;
21+
default:
22+
break;
23+
}
24+
}
25+
26+
void f3(char *p1) {
27+
switch (p1 == "CODEQL") // NON_COMPLIANT
28+
{
29+
case 0:
30+
break;
31+
case 1:
32+
break;
33+
default:
34+
break;
35+
}
36+
}

rule_packages/c/Statements2.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
},
2828
"queries": [
2929
{
30-
"description": "Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement",
30+
"description": "Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement.",
3131
"kind": "problem",
32-
"name": "The goto statement and any of its label shall be declared or enclosed in the same block. ",
32+
"name": "The goto statement and any of its label shall be declared or enclosed in the same block",
3333
"precision": "high",
3434
"severity": "recommendation",
3535
"short_name": "GotoLabelBlockCondition",
@@ -87,13 +87,16 @@
8787
},
8888
"queries": [
8989
{
90-
"description": "",
90+
"description": "An `if-else` construct is more appropriate for boolean controlled expression.",
9191
"kind": "problem",
9292
"name": "A switch-expression shall not have essentially Boolean type",
9393
"precision": "very-high",
9494
"severity": "error",
9595
"short_name": "SwitchExpressionBoolCondition",
96-
"tags": []
96+
"tags": [
97+
"readability",
98+
"maintainability"
99+
]
97100
}
98101
],
99102
"title": "A switch-expression shall not have essentially Boolean type"

0 commit comments

Comments
 (0)