Skip to content

Commit 6977d61

Browse files
committed
Merge branch 'statements1' of github.com:github/codeql-coding-standards into statements1
2 parents 3b86205 + 094f6a0 commit 6977d61

File tree

5 files changed

+71
-11
lines changed

5 files changed

+71
-11
lines changed

c/misra/src/rules/RULE-15-3/GotoLabelBlockCondition.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @id c/misra/goto-label-block-condition
3-
* @name RULE-15-3: The goto statement and any of its label shall be declared or enclosed in the same block.
3+
* @name RULE-15-3: The goto statement and any of its label shall be declared or enclosed in the same block.
44
* @description Any label referenced by a goto statement shall be declared in the same block, or in
55
* any block enclosing the goto statement
66
* @kind problem
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
/**
22
* @id c/misra/switch-clause-number-condition
33
* @name RULE-16-6: Every switch statement shall have at least two switch-clauses
4-
* @description
4+
* @description Switch Statements with a single path are redundant and may cause programming errors.
55
* @kind problem
66
* @precision very-high
7-
* @problem.severity error
7+
* @problem.severity recommendation
88
* @tags external/misra/id/rule-16-6
9+
* maintainability
10+
* readability
911
* external/misra/obligation/required
1012
*/
1113

1214
import cpp
1315
import codingstandards.c.misra
1416

15-
from
17+
from SwitchStmt switch
1618
where
17-
not isExcluded(x, Statements2Package::switchClauseNumberConditionQuery()) and
18-
select
19+
not isExcluded(switch, Statements2Package::switchClauseNumberConditionQuery()) and
20+
count(SwitchCase case |
21+
switch.getASwitchCase() = case and
22+
case.getNextSwitchCase() != case.getFollowingStmt()
23+
) + 1 < 2
24+
select switch, "$@ statement has a single path.", switch, "Switch"
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
No expected results have yet been specified
1+
| test.c:3:3:6:3 | switch (...) ... | $@ statement has a single path. | test.c:3:3:6:3 | switch (...) ... | Switch |
2+
| test.c:8:3:12:3 | switch (...) ... | $@ statement has a single path. | test.c:8:3:12:3 | switch (...) ... | Switch |
3+
| test.c:14:3:19:3 | switch (...) ... | $@ statement has a single path. | test.c:14:3:19:3 | switch (...) ... | Switch |
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
void f1(int p1) {
2+
int i = 0;
3+
switch (p1) { // NON_COMPLIANT
4+
default:
5+
break;
6+
}
7+
8+
switch (p1) { // NON_COMPLIANT
9+
case 1:
10+
default:
11+
break;
12+
}
13+
14+
switch (p1) { // NON_COMPLIANT
15+
case 1:
16+
case 2:
17+
default:
18+
break;
19+
}
20+
21+
switch (p1) { // COMPLIANT
22+
case 1:
23+
i++;
24+
default:
25+
i = 1;
26+
break;
27+
}
28+
29+
switch (p1) { // COMPLIANT
30+
case 1:
31+
i++;
32+
case 2:
33+
i = 2;
34+
default:
35+
i = 1;
36+
break;
37+
}
38+
39+
switch (p1) { // COMPLIANT
40+
case 1:
41+
i++;
42+
case 2:
43+
i = 2;
44+
case 3:
45+
default:
46+
i = 1;
47+
break;
48+
}
49+
}

rule_packages/c/Statements2.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,16 @@
6767
},
6868
"queries": [
6969
{
70-
"description": "",
70+
"description": "Switch Statements with a single path are redundant and may cause programming errors.",
7171
"kind": "problem",
7272
"name": "Every switch statement shall have at least two switch-clauses",
7373
"precision": "very-high",
74-
"severity": "error",
74+
"severity": "recommendation",
7575
"short_name": "SwitchClauseNumberCondition",
76-
"tags": []
76+
"tags": [
77+
"maintainability",
78+
"readability"
79+
]
7780
}
7881
],
7982
"title": "Every switch statement shall have at least two switch-clauses"
@@ -96,4 +99,4 @@
9699
"title": "A switch-expression shall not have essentially Boolean type"
97100
}
98101
}
99-
}
102+
}

0 commit comments

Comments
 (0)