File tree Expand file tree Collapse file tree 4 files changed +44
-6
lines changed
test/query-tests/Variables/unused_local_nonlocal Expand file tree Collapse file tree 4 files changed +44
-6
lines changed Original file line number Diff line number Diff line change @@ -15,20 +15,20 @@ import python
15
15
import Definition
16
16
17
17
predicate unused_local ( Name unused , LocalVariable v ) {
18
- forex ( Definition def |
19
- def .getNode ( ) = unused |
18
+ forex ( Definition def | def .getNode ( ) = unused |
20
19
def .getVariable ( ) = v and
21
20
def .isUnused ( ) and
22
21
not exists ( def .getARedef ( ) ) and
23
22
def .isRelevant ( ) and
23
+ not v = any ( Nonlocal n ) .getAVariable ( ) and
24
24
not exists ( def .getNode ( ) .getParentNode ( ) .( FunctionDef ) .getDefinedFunction ( ) .getADecorator ( ) ) and
25
25
not exists ( def .getNode ( ) .getParentNode ( ) .( ClassDef ) .getDefinedClass ( ) .getADecorator ( ) )
26
26
)
27
27
}
28
28
29
-
30
29
from Name unused , LocalVariable v
31
- where unused_local ( unused , v ) and
32
- // If unused is part of a tuple, count it as unused if all elements of that tuple are unused.
33
- forall ( Name el | el = unused .getParentNode ( ) .( Tuple ) .getAnElt ( ) | unused_local ( el , _) )
30
+ where
31
+ unused_local ( unused , v ) and
32
+ // If unused is part of a tuple, count it as unused if all elements of that tuple are unused.
33
+ forall ( Name el | el = unused .getParentNode ( ) .( Tuple ) .getAnElt ( ) | unused_local ( el , _) )
34
34
select unused , "The value assigned to local variable '" + v .getId ( ) + "' is never used."
Original file line number Diff line number Diff line change
1
+ | variables_test.py:32:9:32:12 | test | The value assigned to local variable 'test' is never used. |
Original file line number Diff line number Diff line change
1
+ Variables/UnusedLocalVariable.ql
Original file line number Diff line number Diff line change
1
+
2
+ # FPs involving nonlocal
3
+
4
+ def nonlocal_fp ():
5
+ test = False
6
+ def set_test ():
7
+ nonlocal test
8
+ test = True
9
+ set_test ()
10
+ if test :
11
+ print ("Test is set." )
12
+
13
+ nonlocal_fp ()
14
+
15
+ def nonlocal_fp2 ():
16
+ test = False
17
+
18
+ def set_test ():
19
+ nonlocal test
20
+ test = True
21
+ set_test ()
22
+ result = 5
23
+ if not test :
24
+ return
25
+ return result
26
+
27
+ def not_fp ():
28
+ test = False
29
+ def nonlocal_test ():
30
+ nonlocal test
31
+ def set_test ():
32
+ test = True
33
+ nonlocal_test ()
34
+ set_test ()
35
+ if test :
36
+ print ("Test is set." )
You can’t perform that action at this time.
0 commit comments