-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathUnused.ql
More file actions
33 lines (30 loc) · 1.28 KB
/
Unused.ql
File metadata and controls
33 lines (30 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* @name Variable is assigned a value that is never read
* @description Assigning a value to a variable that is not used may indicate an error in the code.
* @kind problem
* @id cpp/unused-variable
* @problem.severity warning
* @tags maintainability
* external/cwe/cwe-563
*/
import cpp
// Sometimes it is useful to have a class which is instantiated (on the stack)
// but not otherwise used. This is usually to perform some task and have that
// task automatically reversed when the current scope is left. For example,
// sometimes locking is done this way.
//
// Obviously, such instantiations should not be treated as unused values.
class ScopeUtilityClass extends Class {
Call getAUse() { result = this.getAConstructor().getACallToThisFunction() }
}
from StackVariable v, ControlFlowNode def
where
definition(v, def) and
not definitionUsePair(v, def, _) and
not v.getAnAccess().isAddressOfAccess() and
// parameter initializers are not in the call-graph at the moment
not v.(Parameter).getInitializer().getExpr() = def and
not v.getType().getUnderlyingType() instanceof ReferenceType and
not exists(ScopeUtilityClass util | def = util.getAUse()) and
not def.isInMacroExpansion()
select def, "Variable '" + v.getName() + "' is assigned a value that is never used."