Skip to content

Commit 250e06f

Browse files
committed
Rule 16.4: Consider nested blocks.
1 parent 575f42e commit 250e06f

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

c/misra/src/rules/RULE-16-4/EverySwitchShallHaveDefaultLabel.ql

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
import cpp
1515
import codingstandards.c.misra
1616

17+
Stmt getFirstNonBlockStatement(BlockStmt bs) {
18+
exists(Stmt nextStmt | nextStmt = bs.getStmt(0) |
19+
if nextStmt instanceof BlockStmt
20+
then result = getFirstNonBlockStatement(nextStmt)
21+
else result = nextStmt
22+
)
23+
}
24+
25+
Stmt getFirstStatement(DefaultCase case) {
26+
exists(Stmt next | next = case.getFollowingStmt() |
27+
if next instanceof BlockStmt then result = getFirstNonBlockStatement(next) else result = next
28+
)
29+
}
30+
1731
from SwitchStmt switch, string message
1832
where
1933
not isExcluded(switch, Statements1Package::everySwitchShallHaveDefaultLabelQuery()) and
@@ -22,7 +36,7 @@ where
2236
or
2337
exists(SwitchCase case, BreakStmt break |
2438
switch.getDefaultCase() = case and
25-
case.getFollowingStmt() = break and
39+
getFirstStatement(case) = break and
2640
not exists(Comment comment | comment.getCommentedElement() = break) and
2741
message =
2842
"has default label that does not terminate in a statement or comment before break statement"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
| test.c:4:3:12:3 | switch (...) ... | $@ statement has missing default clause. | test.c:4:3:12:3 | switch (...) ... | Switch |
22
| test.c:13:3:22:3 | switch (...) ... | $@ statement has default label that does not terminate in a statement or comment before break statement | test.c:13:3:22:3 | switch (...) ... | Switch |
3+
| test.c:53:3:60:3 | switch (...) ... | $@ statement has default label that does not terminate in a statement or comment before break statement | test.c:53:3:60:3 | switch (...) ... | Switch |

c/misra/test/rules/RULE-16-4/test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,13 @@ void f1(int p1) {
4949
i++;
5050
break;
5151
}
52+
53+
switch (p1) { // NON_COMPLIANT
54+
case 1:
55+
i++;
56+
break;
57+
default: {
58+
break;
59+
}
60+
}
5261
}

0 commit comments

Comments
 (0)