mirror of
https://github.com/github/codeql.git
synced 2026-05-02 04:05:14 +02:00
Merge pull request #617 from geoffw0/unusedstatic
CPP: Fix false positives in UnusedStaticVariables.ql
This commit is contained in:
16
change-notes/1.20/analysis-cpp.md
Normal file
16
change-notes/1.20/analysis-cpp.md
Normal 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
|
||||
@@ -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"
|
||||
|
||||
@@ -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 |
|
||||
@@ -0,0 +1 @@
|
||||
Best Practices/Unused Entities/UnusedStaticVariables.ql
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user