@@ -14,7 +14,7 @@ import python
1414import Variables.Definition
1515
1616predicate global_name_used ( Module m , Variable name ) {
17- exists ( Name u , GlobalVariable v |
17+ exists ( Name u , GlobalVariable v |
1818 u .uses ( v ) and
1919 v .getId ( ) = name .getId ( ) and
2020 u .getEnclosingModule ( ) = m
@@ -31,8 +31,7 @@ predicate global_name_used(Module m, Variable name) {
3131
3232/** Holds if a module has `__all__` but we don't understand it */
3333predicate all_not_understood ( Module m ) {
34- exists ( GlobalVariable a |
35- a .getId ( ) = "__all__" and a .getScope ( ) = m |
34+ exists ( GlobalVariable a | a .getId ( ) = "__all__" and a .getScope ( ) = m |
3635 // `__all__` is not defined as a simple list
3736 not m .declaredInAll ( _)
3837 or
@@ -43,8 +42,7 @@ predicate all_not_understood(Module m) {
4342
4443predicate imported_module_used_in_doctest ( Import imp ) {
4544 exists ( string modname |
46- imp .getAName ( ) .getAsname ( ) .( Name ) .getId ( ) = modname
47- and
45+ imp .getAName ( ) .getAsname ( ) .( Name ) .getId ( ) = modname and
4846 // Look for doctests containing the patterns:
4947 // >>> …name…
5048 // ... …name…
@@ -60,7 +58,7 @@ predicate imported_module_used_in_typehint(Import imp) {
6058 exists ( string modname , Location loc |
6159 imp .getAName ( ) .getAsname ( ) .( Name ) .getId ( ) = modname and
6260 loc .getFile ( ) = imp .getScope ( ) .( Module ) .getFile ( )
63- |
61+ |
6462 // Look for type hints containing the patterns:
6563 // # type: …name…
6664 exists ( Comment typehint |
@@ -73,51 +71,36 @@ predicate imported_module_used_in_typehint(Import imp) {
7371 annotation = any ( Arguments a ) .getAnAnnotation ( )
7472 or
7573 annotation = any ( AnnAssign a ) .getAnnotation ( )
76- |
74+ |
7775 annotation .pointsTo ( Value:: forString ( typehint ) ) and
7876 loc = annotation .getLocation ( ) and
7977 typehint .regexpMatch ( ".*\\b" + modname + "\\b.*" )
8078 )
8179 )
8280}
8381
84-
8582predicate unused_import ( Import imp , Variable name ) {
86- ( ( Name ) imp .getAName ( ) .getAsname ( ) ) .getVariable ( ) = name
87- and
88- not imp .getAnImportedModuleName ( ) = "__future__"
89- and
90- not imp .getEnclosingModule ( ) .declaredInAll ( name .getId ( ) )
91- and
92- imp .getScope ( ) = imp .getEnclosingModule ( )
93- and
94- not global_name_used ( imp .getScope ( ) , name )
95- and
83+ imp .getAName ( ) .getAsname ( ) .( Name ) .getVariable ( ) = name and
84+ not imp .getAnImportedModuleName ( ) = "__future__" and
85+ not imp .getEnclosingModule ( ) .declaredInAll ( name .getId ( ) ) and
86+ imp .getScope ( ) = imp .getEnclosingModule ( ) and
87+ not global_name_used ( imp .getScope ( ) , name ) and
9688 // Imports in `__init__.py` are used to force module loading
97- not imp .getEnclosingModule ( ) .isPackageInit ( )
98- and
89+ not imp .getEnclosingModule ( ) .isPackageInit ( ) and
9990 // Name may be imported for use in epytext documentation
100- not exists ( Comment cmt |
101- cmt .getText ( ) .matches ( "%L{" + name .getId ( ) + "}%" ) |
91+ not exists ( Comment cmt | cmt .getText ( ) .matches ( "%L{" + name .getId ( ) + "}%" ) |
10292 cmt .getLocation ( ) .getFile ( ) = imp .getLocation ( ) .getFile ( )
103- )
104- and
105- not name_acceptable_for_unused_variable ( name )
106- and
93+ ) and
94+ not name_acceptable_for_unused_variable ( name ) and
10795 // Assume that opaque `__all__` includes imported module
108- not all_not_understood ( imp .getEnclosingModule ( ) )
109- and
110- not imported_module_used_in_doctest ( imp )
111- and
112- not imported_module_used_in_typehint ( imp )
113- and
114- // Only consider import statements that actually point-to something (possibly an unknown module).
96+ not all_not_understood ( imp .getEnclosingModule ( ) ) and
97+ not imported_module_used_in_doctest ( imp ) and
98+ not imported_module_used_in_typehint ( imp ) and
99+ // Only consider import statements that actually point-to something (possibly an unknown module).
115100 // If this is not the case, it's likely that the import statement never gets executed.
116101 imp .getAName ( ) .getValue ( ) .pointsTo ( _)
117102}
118103
119-
120104from Stmt s , Variable name
121105where unused_import ( s , name )
122106select s , "Import of '" + name .getId ( ) + "' is not used."
123-
0 commit comments