mirror of
https://github.com/github/codeql.git
synced 2026-06-17 10:51:09 +02:00
Convert Python qlref tests to inline expectations
This commit is contained in:
@@ -1 +1,2 @@
|
||||
Variables/Global.ql
|
||||
query: Variables/Global.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Variables/GlobalAtModuleLevel.ql
|
||||
query: Variables/GlobalAtModuleLevel.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Variables/ShadowBuiltin.ql
|
||||
query: Variables/ShadowBuiltin.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Variables/ShadowGlobal.ql
|
||||
query: Variables/ShadowGlobal.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -4,14 +4,14 @@ __all__ = [ 'is_used_var1' ]
|
||||
#Shadow Builtin
|
||||
|
||||
def sh1(x):
|
||||
len = x + 2 #Shadows
|
||||
len = x + 2 # $ Alert[py/local-shadows-builtin] #Shadows
|
||||
len = x + 0 # no shadowing warning for 2nd def
|
||||
return len
|
||||
|
||||
#Shadow Global
|
||||
|
||||
def sh2(x):
|
||||
sh1 = x + 1 #Shadows
|
||||
sh1 = x + 1 # $ Alert[py/local-shadows-global] #Shadows
|
||||
sh1 = x + 0 # no shadowing warning for 2nd def
|
||||
return sh1
|
||||
|
||||
@@ -54,14 +54,14 @@ def func():
|
||||
return is_used_var2
|
||||
|
||||
#Redundant global declaration
|
||||
global g_x
|
||||
global g_x # $ Alert[py/redundant-global-declaration]
|
||||
|
||||
g_x = 0
|
||||
|
||||
#Use global
|
||||
|
||||
def uses_global(arg):
|
||||
global g_x
|
||||
global g_x # $ Alert[py/use-of-global]
|
||||
g_x = arg
|
||||
|
||||
use(g_x)
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Variables/MultiplyDefined.ql
|
||||
query: Variables/MultiplyDefined.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
|
||||
#Multiple declarations
|
||||
|
||||
def mult(a):
|
||||
x = 1
|
||||
def mult(a): # $ Alert
|
||||
x = 1 # $ Alert
|
||||
y = a
|
||||
x = 2
|
||||
#Need to use x, otherwise it is ignored
|
||||
@@ -25,7 +25,7 @@ def _double_loop(seq):
|
||||
for i in seq:
|
||||
pass
|
||||
|
||||
class Mult(object):
|
||||
class Mult(object): # $ Alert
|
||||
|
||||
pass
|
||||
|
||||
@@ -49,7 +49,7 @@ def isStr(s):
|
||||
# 'bad' actually *is* always redefined before being read.
|
||||
def have_nosmp():
|
||||
try:
|
||||
bad = os.environ['NPY_NOSMP']
|
||||
bad = os.environ['NPY_NOSMP'] # $ Alert
|
||||
bad = 1
|
||||
except KeyError:
|
||||
bad = 0
|
||||
@@ -64,7 +64,7 @@ def simple_try(foo):
|
||||
|
||||
def try_with_else(foo):
|
||||
try:
|
||||
bad = foo.bar
|
||||
bad = foo.bar # $ Alert
|
||||
except AttributeError:
|
||||
raise
|
||||
else:
|
||||
@@ -114,7 +114,7 @@ def odasa4166(cond):
|
||||
def odasa5315():
|
||||
x, y = foo() # OK as y is used
|
||||
use(y)
|
||||
x, y = bar() # Not OK as neither x nor y are used.
|
||||
x, y = bar() # $ Alert # Not OK as neither x nor y are used.
|
||||
x, y = baz() # OK as both used
|
||||
return x + y
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Variables/UndefinedExport.ql
|
||||
query: Variables/UndefinedExport.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -19,12 +19,12 @@ def f(parameter):
|
||||
helper # Explicitly as import
|
||||
a # Imlicitly from ud_helper
|
||||
defined
|
||||
ug2 # ERROR
|
||||
e # ERROR Defined in ud_helper, but not in __all__
|
||||
ug2 # $ Alert[py/undefined-global-variable] # ERROR
|
||||
e # $ Alert[py/undefined-global-variable] # ERROR Defined in ud_helper, but not in __all__
|
||||
int
|
||||
float
|
||||
__file__ #OK all files have __file__ defined
|
||||
__path__ #ERROR only modules have __path__ defined
|
||||
__path__ # $ Alert[py/undefined-global-variable] #ERROR only modules have __path__ defined
|
||||
|
||||
len #Ok defined in builtins
|
||||
monkey1 #Ok monkey-patched builtins
|
||||
@@ -120,7 +120,7 @@ class Cls(object):
|
||||
pfp3 += 1
|
||||
|
||||
def only_report_once():
|
||||
ug3
|
||||
ug3 # $ Alert[py/undefined-global-variable]
|
||||
ug3
|
||||
ug3
|
||||
ug3
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Variables/UndefinedGlobal.ql
|
||||
query: Variables/UndefinedGlobal.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
class C:
|
||||
|
||||
def m1(self):
|
||||
y = ug1
|
||||
y = ug1 # $ Alert[py/undefined-global-variable]
|
||||
x = 1
|
||||
return y
|
||||
|
||||
@@ -10,16 +10,16 @@ class C:
|
||||
return p
|
||||
|
||||
def m3(self, x1):
|
||||
return u2
|
||||
return u2 # $ Alert[py/uninitialized-local-variable]
|
||||
u2 = x1
|
||||
|
||||
def m4(self, x2):
|
||||
if x2:
|
||||
u3 = 1
|
||||
return u3
|
||||
return u3 # $ Alert[py/uninitialized-local-variable]
|
||||
|
||||
def f():
|
||||
y = ug1
|
||||
y = ug1 # $ Alert[py/undefined-global-variable]
|
||||
x = 1
|
||||
return y
|
||||
|
||||
@@ -34,7 +34,7 @@ def q(x4):
|
||||
|
||||
def j(u4):
|
||||
del u4
|
||||
return u4
|
||||
return u4 # $ Alert[py/uninitialized-local-variable]
|
||||
|
||||
def k(x5):
|
||||
x5 + 1
|
||||
@@ -43,7 +43,7 @@ def k(x5):
|
||||
def m(x6):
|
||||
if x6:
|
||||
u6 = 1
|
||||
u6
|
||||
u6 # $ Alert[py/uninitialized-local-variable]
|
||||
#The following are not uninitialized, but unreachable.
|
||||
u6
|
||||
u6
|
||||
@@ -66,13 +66,13 @@ def check_del(cond):
|
||||
del u8
|
||||
else:
|
||||
pass
|
||||
u8
|
||||
u8 # $ Alert[py/uninitialized-local-variable]
|
||||
if cond:
|
||||
u9 = 1
|
||||
del u9
|
||||
else:
|
||||
u9 = 2
|
||||
u9
|
||||
u9 # $ Alert[py/uninitialized-local-variable]
|
||||
if cond:
|
||||
x10 = 1
|
||||
del x10
|
||||
@@ -82,10 +82,10 @@ def check_del(cond):
|
||||
x10
|
||||
u11 = 1
|
||||
del u11
|
||||
u11
|
||||
u11 # $ Alert[py/uninitialized-local-variable]
|
||||
u12 = "hi"
|
||||
del u12
|
||||
del u12
|
||||
del u12 # $ Alert[py/uninitialized-local-variable]
|
||||
|
||||
#x will always be defined.
|
||||
def const_range():
|
||||
@@ -116,7 +116,7 @@ def use_def_conditional(cond4, cond5):
|
||||
u14 = 1
|
||||
x16 = 2
|
||||
if cond5:
|
||||
return u14
|
||||
return u14 # $ Alert[py/uninitialized-local-variable]
|
||||
|
||||
|
||||
def init_and_set_flag_in_try(f):
|
||||
@@ -148,7 +148,7 @@ def split_not_OK():
|
||||
except:
|
||||
cond = not False
|
||||
if not not cond:
|
||||
return u19
|
||||
return u19 # $ Alert[py/uninitialized-local-variable]
|
||||
|
||||
def double_is_none(x):
|
||||
if x is not None:
|
||||
@@ -160,7 +160,7 @@ def double_is_none(x):
|
||||
|
||||
#ODASA-4241
|
||||
def def_in_post_loop(seq):
|
||||
j(x)
|
||||
j(x) # $ Alert[py/uninitialized-local-variable]
|
||||
x = []
|
||||
for p in seq:
|
||||
x = p
|
||||
@@ -173,9 +173,9 @@ def f(cond1, cond2):
|
||||
else:
|
||||
y = 1
|
||||
if cond2:
|
||||
return x
|
||||
return x # $ Alert[py/uninitialized-local-variable]
|
||||
else:
|
||||
return y
|
||||
return y # $ Alert[py/uninitialized-local-variable]
|
||||
|
||||
def needs_splitting(var):
|
||||
if var:
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Variables/UninitializedLocal.ql
|
||||
query: Variables/UninitializedLocal.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import dotted
|
||||
|
||||
__all__ = ["foo", "bar", "baz", "not_defined"]
|
||||
__all__ = ["foo", "bar", "baz", "not_defined"] # $ Alert[py/undefined-export]
|
||||
|
||||
|
||||
@dotted.decorator
|
||||
def foo():
|
||||
pass
|
||||
|
||||
@undotted_decorator
|
||||
@undotted_decorator # $ Alert[py/undefined-global-variable]
|
||||
def bar():
|
||||
pass
|
||||
|
||||
@not_imported.but_dotted
|
||||
@not_imported.but_dotted # $ Alert[py/undefined-global-variable]
|
||||
def baz():
|
||||
pass
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
__all__ = ["foo", "bar", "baz", "quux", "blat", "frob", "nosuch", "i_got_it_elsewhere"]
|
||||
__all__ = ["foo", "bar", "baz", "quux", "blat", "frob", "nosuch", "i_got_it_elsewhere"] # $ Alert[py/undefined-export]
|
||||
|
||||
with open("foo.txt") as f:
|
||||
foo = f.read()
|
||||
|
||||
@@ -8,7 +8,7 @@ def f(cond1, cond2):
|
||||
except Exception:
|
||||
if cond2:
|
||||
var = 7
|
||||
if var == 1:
|
||||
if var == 1: # $ Alert[py/uninitialized-local-variable]
|
||||
var = var + 1
|
||||
elif var == 2:
|
||||
var +- 3
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Variables/SuspiciousUnusedLoopIterationVariable.ql
|
||||
query: Variables/SuspiciousUnusedLoopIterationVariable.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Variables/UnusedLocalVariable.ql
|
||||
query: Variables/UnusedLocalVariable.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Variables/UnusedModuleVariable.ql
|
||||
query: Variables/UnusedModuleVariable.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Variables/UnusedParameter.ql
|
||||
query: Variables/UnusedParameter.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
#Unused
|
||||
def fail():
|
||||
for t in [TypeA, TypeB]:
|
||||
for t in [TypeA, TypeB]: # $ Alert[py/unused-loop-variable]
|
||||
x = TypeA()
|
||||
run_test(x)
|
||||
|
||||
@@ -63,19 +63,19 @@ def OK8(seq, output):
|
||||
#Not OK -- Use a constant, but also a variable
|
||||
def fail2(sequence):
|
||||
for x in sequence:
|
||||
for y in sequence:
|
||||
for y in sequence: # $ Alert[py/unused-loop-variable]
|
||||
do_something(x+1)
|
||||
|
||||
def fail3(sequence):
|
||||
for x in sequence:
|
||||
do_something(x+1)
|
||||
for y in sequence:
|
||||
for y in sequence: # $ Alert[py/unused-loop-variable]
|
||||
do_something(x+1)
|
||||
|
||||
def fail4(coll, sequence):
|
||||
while coll:
|
||||
x = coll.pop()
|
||||
for s in sequence:
|
||||
for s in sequence: # $ Alert[py/unused-loop-variable]
|
||||
do_something(x+1)
|
||||
|
||||
#OK See ODASA-4153 and ODASA-4533
|
||||
@@ -103,7 +103,7 @@ def kwargs_is_a_use(seq):
|
||||
|
||||
#A deletion is a use, but this is almost certainly an error
|
||||
def cleanup(sessions):
|
||||
for sess in sessions:
|
||||
for sess in sessions: # $ Alert[py/unused-loop-variable]
|
||||
# Original code had some comment about deleting sessions
|
||||
del sess
|
||||
|
||||
|
||||
@@ -22,11 +22,11 @@ __hidden_marker = False
|
||||
|
||||
#Unused parameter, local and global
|
||||
|
||||
def u1(x):
|
||||
def u1(x): # $ Alert[py/unused-parameter]
|
||||
return 0
|
||||
|
||||
def u2():
|
||||
x = 1
|
||||
x = 1 # $ Alert[py/unused-local-variable]
|
||||
return 1
|
||||
|
||||
#These parameters are OK due to (potential overriding)
|
||||
@@ -45,8 +45,8 @@ class D(C):
|
||||
pass
|
||||
|
||||
#Unused module variable
|
||||
not_used_var1 = 17
|
||||
not_used_var2 = 18
|
||||
not_used_var1 = 17 # $ Alert[py/unused-global-variable]
|
||||
not_used_var2 = 18 # $ Alert[py/unused-global-variable]
|
||||
is_used_var1 = 19
|
||||
is_used_var2 = 20
|
||||
|
||||
@@ -83,21 +83,21 @@ def f(t):
|
||||
|
||||
# Entirely unused tuple
|
||||
|
||||
a,b,c = t
|
||||
a,b,c = t # $ Alert[py/unused-global-variable]
|
||||
|
||||
def f(t):
|
||||
a,b,c = t
|
||||
a,b,c = t # $ Alert[py/unused-local-variable]
|
||||
use(t)
|
||||
|
||||
def second_def_undefined():
|
||||
var = 0
|
||||
use(var)
|
||||
var = 1 # unused.
|
||||
var = 1 # $ Alert[py/unused-local-variable] # unused.
|
||||
|
||||
#And gloablly
|
||||
glob_var = 0
|
||||
use(glob_var)
|
||||
glob_var = 1 # unused
|
||||
glob_var = 1 # $ Alert[py/unused-global-variable] # unused
|
||||
|
||||
|
||||
|
||||
@@ -130,7 +130,7 @@ def decorated_inner_function():
|
||||
#FP observed
|
||||
def test_dict_unpacking(queryset, field_name, value):
|
||||
#True positive
|
||||
for tag in value.split(','):
|
||||
for tag in value.split(','): # $ Alert[py/unused-loop-variable]
|
||||
queryset = queryset.filter(**{field_name + '__name': tag1})
|
||||
return queryset
|
||||
#False positive
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Variables/UnusedLocalVariable.ql
|
||||
query: Variables/UnusedLocalVariable.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -29,7 +29,7 @@ def not_fp():
|
||||
def nonlocal_test():
|
||||
nonlocal test
|
||||
def set_test():
|
||||
test = True
|
||||
test = True # $ Alert
|
||||
nonlocal_test()
|
||||
set_test()
|
||||
if test:
|
||||
|
||||
Reference in New Issue
Block a user