Skip to content

Commit 0a496c1

Browse files
authored
Merge pull request #617 from geoffw0/unusedstatic
CPP: Fix false positives in UnusedStaticVariables.ql
2 parents 75842fe + f6a8757 commit 0a496c1

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

change-notes/1.20/analysis-cpp.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Improvements to C/C++ analysis
2+
3+
## General improvements
4+
5+
## New queries
6+
7+
| **Query** | **Tags** | **Purpose** |
8+
|-----------------------------|-----------|--------------------------------------------------------------------|
9+
10+
## Changes to existing queries
11+
12+
| **Query** | **Expected impact** | **Change** |
13+
|----------------------------|------------------------|------------------------------------------------------------------|
14+
| Unused static variable (`cpp/unused-static-variable`) | Fewer false positive results | Variables with the attribute `unused` are now excluded from the query. |
15+
16+
## Changes to QL libraries

cpp/ql/src/Best Practices/Unused Entities/UnusedStaticVariables.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ where v.isStatic()
2525
and not v instanceof MemberVariable
2626
and not declarationHasSideEffects(v)
2727
and not v.getAnAttribute().hasName("used")
28+
and not v.getAnAttribute().hasName("unused")
2829
select v, "Static variable " + v.getName() + " is never read"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:7:12:7:21 | staticVar5 | Static variable staticVar5 is never read |
2+
| test.cpp:8:12:8:21 | staticVar6 | Static variable staticVar6 is never read |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Best Practices/Unused Entities/UnusedStaticVariables.ql
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
int globalVar; // GOOD (not static)
3+
static int staticVar1; // GOOD (used)
4+
static int staticVar2; // GOOD (used)
5+
static int staticVar3 = 3; // GOOD (used)
6+
static int staticVar4 = staticVar3; // GOOD (used)
7+
static int staticVar5; // BAD (unused)
8+
static int staticVar6 = 6; // BAD (unused)
9+
static __attribute__((__unused__)) int staticVar7; // GOOD (unused but this is expected)
10+
11+
void f()
12+
{
13+
int *ptr = &staticVar4;
14+
15+
staticVar1 = staticVar2;
16+
(*ptr) = 0;
17+
}
18+

0 commit comments

Comments
 (0)