forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShadowBuiltin.ql
More file actions
67 lines (62 loc) · 1.69 KB
/
ShadowBuiltin.ql
File metadata and controls
67 lines (62 loc) · 1.69 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
62
63
64
65
66
67
/**
* @name Builtin shadowed by local variable
* @description Defining a local variable with the same name as a built-in object
* makes the built-in object unusable within the current scope and makes the code
* more difficult to read.
* @kind problem
* @tags maintainability
* readability
* @problem.severity recommendation
* @sub-severity low
* @precision medium
* @id py/local-shadows-builtin
*/
import python
import Shadowing
import semmle.python.types.Builtins
predicate allow_list(string name) {
/* These are rarely used and thus unlikely to be confusing */
name = "iter" or
name = "next" or
name = "input" or
name = "file" or
name = "apply" or
name = "slice" or
name = "buffer" or
name = "coerce" or
name = "intern" or
name = "exit" or
name = "quit" or
name = "license" or
/* These are short and/or hard to avoid */
name = "dir" or
name = "id" or
name = "max" or
name = "min" or
name = "sum" or
name = "cmp" or
name = "chr" or
name = "ord" or
name = "bytes" or
name = "_"
}
predicate shadows(Name d, string name, Function scope, int line) {
exists(LocalVariable l |
d.defines(l) and
l.getId() = name and
exists(Builtin::builtin(l.getId()))
) and
d.getScope() = scope and
d.getLocation().getStartLine() = line and
not allow_list(name) and
not optimizing_parameter(d)
}
predicate first_shadowing_definition(Name d, string name) {
exists(int first, Scope scope |
shadows(d, name, scope, first) and
first = min(int line | shadows(_, name, scope, line))
)
}
from Name d, string name
where first_shadowing_definition(d, name)
select d, "Local variable " + name + " shadows a builtin variable."