-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathMultipleImports.ql
More file actions
47 lines (43 loc) · 1.53 KB
/
MultipleImports.ql
File metadata and controls
47 lines (43 loc) · 1.53 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
/**
* @name Module is imported more than once
* @description Importing a module a second time has no effect and impairs readability
* @kind problem
* @tags quality
* maintainability
* useless-code
* @problem.severity recommendation
* @sub-severity high
* @precision very-high
* @id py/repeated-import
*/
import python
predicate is_simple_import(Import imp) { not exists(Attribute a | imp.contains(a)) }
predicate double_import(Import original, Import duplicate, Module m) {
original != duplicate and
is_simple_import(original) and
is_simple_import(duplicate) and
/* Imports import the same thing */
exists(ImportExpr e1, ImportExpr e2 |
e1.getName() = m.getName() and
e2.getName() = m.getName() and
e1 = original.getAName().getValue() and
e2 = duplicate.getAName().getValue()
) and
original.getAName().getAsname().(Name).getId() = duplicate.getAName().getAsname().(Name).getId() and
exists(Module enclosing |
original.getScope() = enclosing and
duplicate.getEnclosingModule() = enclosing and
(
/* Duplicate is not at top level scope */
duplicate.getScope() != enclosing
or
/* Original dominates duplicate */
original.getAnEntryNode().dominates(duplicate.getAnEntryNode())
)
)
}
from Import original, Import duplicate, Module m
where double_import(original, duplicate, m)
select duplicate,
"This import of module " + m.getName() + " is redundant, as it was previously imported $@.",
original, "on line " + original.getLocation().getStartLine().toString()