mirror of
https://github.com/github/codeql.git
synced 2026-03-17 04:56:58 +01:00
Merge pull request #835 from markshannon/python-compare-is-enum
Python: Fix 'comparison using is' query to account for enum members.
This commit is contained in:
@@ -107,6 +107,20 @@ predicate invalid_portable_is_comparison(Compare comp, Cmpop op, ClassObject cls
|
||||
left.refersTo(obj) and right.refersTo(obj) and
|
||||
exists(ImmutableLiteral il | il.getLiteralObject() = obj)
|
||||
)
|
||||
and
|
||||
/* OK to use 'is' when comparing with a member of an enum */
|
||||
not exists(Expr left, Expr right, AstNode origin |
|
||||
comp.compares(left, op, right) and
|
||||
enum_member(origin) |
|
||||
left.refersTo(_, origin) or right.refersTo(_, origin)
|
||||
)
|
||||
}
|
||||
|
||||
private predicate enum_member(AstNode obj) {
|
||||
exists(ClassObject cls, AssignStmt asgn |
|
||||
cls.getASuperType().getName() = "Enum" |
|
||||
cls.getPyClass() = asgn.getScope() and
|
||||
asgn.getValue() = obj
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
|
||||
|
||||
#ODASA-4519
|
||||
|
||||
#OK as we are using identity tests for unique objects
|
||||
V2 = "v2"
|
||||
V3 = "v3"
|
||||
@@ -85,3 +85,21 @@ def both_sides_known(zero_based="auto", query_id=False):
|
||||
if zero_based is False: # False positive here
|
||||
pass
|
||||
|
||||
#Avoid depending on enum back port for Python 2 tests:
|
||||
class Enum(object):
|
||||
pass
|
||||
|
||||
class MyEnum(Enum):
|
||||
|
||||
memberA = None
|
||||
memberB = 10
|
||||
memberC = ("Hello", "World")
|
||||
|
||||
def comp_enum(x):
|
||||
if x is MyEnum.memberA:
|
||||
return
|
||||
if x is MyEnum.memberB:
|
||||
return
|
||||
if x is MyEnum.memberC:
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user