Skip to content

Commit 57e164c

Browse files
committed
DeadCode: Add MISRA C 2012 Rule 2.3
Adds a query to check for unused type declarations, based on the shared library. Note: the expected results for the C++ test case have been updated to include a compiler generated class. This is because shared queries do not properly apply the exclusion mechanism. See: github/coding-standards-team#1126
1 parent 6ed0ebb commit 57e164c

File tree

7 files changed

+93
-6
lines changed

7 files changed

+93
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| file://:0:0:0:0 | __va_list_tag | Type declaration __va_list_tag is not used. |
2+
| test.c:4:8:4:8 | A | Type declaration A is not used. |
3+
| test.c:7:18:7:18 | D | Type declaration D is not used. |
4+
| test.c:30:11:30:11 | R | Type declaration R is not used. |
5+
| test.c:43:12:43:12 | struct <unnamed> | Type declaration struct <unnamed> is not used. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.unusedtypedeclarations.UnusedTypeDeclarations
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// NOTICE: THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C++ TEST CASE AND CHANGES
2+
// SHOULD BE REFLECTED THERE AS WELL.
3+
4+
struct A {}; // NON_COMPLIANT - unused
5+
6+
struct C {}; // COMPLIANT - used in the type def
7+
typedef struct C D; // NON_COMPLIANT - typedef itself not used
8+
9+
struct F {}; // COMPLIANT - used as a global function return type
10+
11+
struct F test_return_value() {
12+
struct F f;
13+
return f;
14+
}
15+
16+
struct G {}; // COMPLIANT - used as a global function parameter type
17+
18+
void test_global_function(struct G g) {}
19+
20+
enum M { C1, C2, C3 }; // COMPLIANT - used in an enum type access below
21+
22+
void test_enum_access() {
23+
int i = C1;
24+
}
25+
26+
struct O {}; // COMPLIANT - used in typedef below
27+
28+
typedef struct O P; // COMPLIANT - used in typedef below
29+
typedef P Q; // COMPLIANT - used in function below
30+
typedef Q R; // NON_COMPLIANT - never used
31+
32+
Q test_type_def() {}
33+
34+
struct { // COMPLIANT - used in type definition
35+
union { // COMPLIANT - f1 and f3 is accessed
36+
struct { // COMPLIANT - f1 is accessed
37+
int f1;
38+
};
39+
struct { // COMPLIANT - f3 is accessed
40+
float f2;
41+
float f3;
42+
};
43+
struct { // NON_COMPLIANT - f4 is never accessed
44+
long f4;
45+
};
46+
};
47+
int f5;
48+
} s;
49+
50+
void test_nested_struct() {
51+
s.f1;
52+
s.f3;
53+
s.f5;
54+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @id c/misra/unused-type-declarations
3+
* @name RULE-2-3: A project should not contain unused type declarations
4+
* @description Unused type 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-3
10+
* readability
11+
* maintainability
12+
* external/misra/obligation/advisory
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.rules.unusedtypedeclarations.UnusedTypeDeclarations
18+
19+
class UnusedTypeDeclarationsQuery extends UnusedTypeDeclarationsSharedQuery {
20+
UnusedTypeDeclarationsQuery() { this = DeadCodePackage::unusedTypeDeclarationsQuery() }
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/unusedtypedeclarations/UnusedTypeDeclarations.ql
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
| test.cpp:1:7:1:7 | A | Type declaration A is not used. |
2-
| test.cpp:3:7:3:7 | B | Type declaration B is not used. |
3-
| test.cpp:10:11:10:11 | D | Type declaration D is not used. |
4-
| test.cpp:74:11:74:11 | R | Type declaration R is not used. |
5-
| test.cpp:87:12:87:12 | struct <unnamed> | Type declaration struct <unnamed> is not used. |
6-
| test.cpp:108:29:108:30 | AA | Type declaration AA is not used. |
1+
| file://:0:0:0:0 | __va_list_tag | Type declaration __va_list_tag is not used. |
2+
| test.cpp:4:7:4:7 | A | Type declaration A is not used. |
3+
| test.cpp:6:7:6:7 | B | Type declaration B is not used. |
4+
| test.cpp:13:11:13:11 | D | Type declaration D is not used. |
5+
| test.cpp:77:11:77:11 | R | Type declaration R is not used. |
6+
| test.cpp:90:12:90:12 | struct <unnamed> | Type declaration struct <unnamed> is not used. |
7+
| test.cpp:111:29:111:30 | AA | Type declaration AA is not used. |

cpp/common/test/rules/unusedtypedeclarations/test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// NOTICE: SOME OF THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C TEST CASE AND CHANGES
2+
// SHOULD BE REFLECTED THERE AS WELL.
3+
14
class A {}; // NON_COMPLIANT - unused
25

36
class B { // NON_COMPLIANT - only used within itself

0 commit comments

Comments
 (0)