forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalScopeVariableShadowsMember.ql
More file actions
61 lines (55 loc) · 1.98 KB
/
LocalScopeVariableShadowsMember.ql
File metadata and controls
61 lines (55 loc) · 1.98 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @name Local scope variable shadows member
* @description A local scope variable that shadows a member with the same name can be confusing,
* and can result in logic errors.
* @kind problem
* @problem.severity recommendation
* @precision high
* @id cs/local-shadows-member
* @tags quality
* maintainability
* readability
*/
import csharp
pragma[noinline]
private string localVarInType(ValueOrRefType t, LocalScopeVariable v) {
v.getCallable().getDeclaringType() = t and
result = v.getName() and
not v instanceof ImplicitAccessorParameter
}
private string memberInType(ValueOrRefType t, Member m) {
t.hasMember(m) and
result = m.getName() and
not m instanceof Method and
not m instanceof Constructor and
not m instanceof NestedType and
t.isSourceDeclaration()
}
private predicate acceptableShadowing(LocalScopeVariable v, Member m) {
exists(ValueOrRefType t | localVarInType(t, v) = memberInType(t, m) |
// If the callable declaring the local also accesses the shadowed member
// using an explicit `this` qualifier, the shadowing is likely deliberate.
exists(MemberAccess ma | ma.getTarget() = m |
ma.getEnclosingCallable() = v.getCallable() and
ma.targetIsLocalInstance() and
not ma.getQualifier().isImplicit()
)
or
t.getAConstructor().getAParameter() = v
or
// Record types have auto-generated Deconstruct methods, which declare an out parameter
// with the same name as the property field(s).
t.(RecordType).getAMethod("Deconstruct").getAParameter() = v
)
}
private predicate shadowing(ValueOrRefType t, LocalScopeVariable v, Member m) {
localVarInType(t, v) = memberInType(t, m) and
not acceptableShadowing(v, m)
}
from LocalScopeVariable v, Callable c, ValueOrRefType t, Member m
where
c = v.getCallable() and
shadowing(t, v, m) and
(c.(Modifiable).isStatic() implies m.isStatic())
select v, "Local scope variable '" + v.getName() + "' shadows $@.", m,
t.getName() + "." + m.getName()