File tree Expand file tree Collapse file tree 4 files changed +46
-0
lines changed Expand file tree Collapse file tree 4 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @id c/misra/unused-label-declaration
3+ * @name RULE-2-6: A function should not contain unused label declarations
4+ * @description Unused label declarations are either redundant or indicate a possible mistake on the
5+ * part of the programmer.
6+ * @kind problem
7+ * @precision very-high
8+ * @problem.severity warning
9+ * @tags external/misra/id/rule-2-6
10+ * readability
11+ * maintainability
12+ * external/misra/obligation/advisory
13+ */
14+
15+ import cpp
16+ import codingstandards.c.misra
17+
18+ from LabelStmt label
19+ where
20+ not isExcluded ( label , DeadCodePackage:: unusedLabelDeclarationQuery ( ) ) and
21+ // No GotoStmt jumps to this label
22+ not exists ( GotoStmt gs | gs .hasName ( ) and gs .getTarget ( ) = label ) and
23+ // The address of the label is never taken
24+ not exists ( LabelLiteral literal | literal .getLabel ( ) = label )
25+ select label , "Label " + label .getName ( ) + " is unused."
Original file line number Diff line number Diff line change 1+ | test.c:2:1:2:13 | label ...: | Label dead_label_1 is unused. |
2+ | test.c:6:1:6:13 | label ...: | Label dead_label_2 is unused. |
3+ | test.c:8:1:8:13 | label ...: | Label dead_label_3 is unused. |
Original file line number Diff line number Diff line change 1+ rules/RULE-2-6/UnusedLabelDeclaration.ql
Original file line number Diff line number Diff line change 1+ void test1 (int p1 ) {
2+ dead_label_1 : // NON_COMPLIANT
3+ live_label_1 : // COMPLIANT
4+ int x = 0 ;
5+ live_label_2 : // COMPLIANT
6+ dead_label_2 : // NON_COMPLIANT
7+ int y = 0 ;
8+ dead_label_3 : // NON_COMPLIANT
9+ int z = 0 ;
10+
11+ if (p1 > 1 ) {
12+ goto live_label_1 ;
13+ }
14+
15+ // Taking the address of a label is sufficient to make it "live"
16+ void * label_ptr = & & live_label_2 ;
17+ }
You can’t perform that action at this time.
0 commit comments