Merge pull request #617 from geoffw0/unusedstatic

CPP: Fix false positives in UnusedStaticVariables.ql
This commit is contained in:
Jonas Jensen
2018-12-06 14:09:52 +01:00
committed by GitHub
5 changed files with 38 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
# Improvements to C/C++ analysis
## General improvements
## New queries
| **Query** | **Tags** | **Purpose** |
|-----------------------------|-----------|--------------------------------------------------------------------|
## Changes to existing queries
| **Query** | **Expected impact** | **Change** |
|----------------------------|------------------------|------------------------------------------------------------------|
| Unused static variable (`cpp/unused-static-variable`) | Fewer false positive results | Variables with the attribute `unused` are now excluded from the query. |
## Changes to QL libraries

View File

@@ -25,4 +25,5 @@ where v.isStatic()
and not v instanceof MemberVariable
and not declarationHasSideEffects(v)
and not v.getAnAttribute().hasName("used")
and not v.getAnAttribute().hasName("unused")
select v, "Static variable " + v.getName() + " is never read"

View File

@@ -0,0 +1,2 @@
| test.cpp:7:12:7:21 | staticVar5 | Static variable staticVar5 is never read |
| test.cpp:8:12:8:21 | staticVar6 | Static variable staticVar6 is never read |

View File

@@ -0,0 +1 @@
Best Practices/Unused Entities/UnusedStaticVariables.ql

View File

@@ -0,0 +1,18 @@
int globalVar; // GOOD (not static)
static int staticVar1; // GOOD (used)
static int staticVar2; // GOOD (used)
static int staticVar3 = 3; // GOOD (used)
static int staticVar4 = staticVar3; // GOOD (used)
static int staticVar5; // BAD (unused)
static int staticVar6 = 6; // BAD (unused)
static __attribute__((__unused__)) int staticVar7; // GOOD (unused but this is expected)
void f()
{
int *ptr = &staticVar4;
staticVar1 = staticVar2;
(*ptr) = 0;
}