Skip to content

Commit 6e50a0e

Browse files
committed
Python: Modernise the py/explicit-return-in-init query.
Add explicit test case to show that we don't doulbe report this problem.
1 parent f047968 commit 6e50a0e

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

python/ql/src/Functions/ExplicitReturnInInit.ql

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
import python
1414

1515
from Return r
16-
where exists(Function init | init.isInitMethod() and
17-
r.getScope() = init and exists(r.getValue())) and
18-
not r.getValue() instanceof None and
19-
not exists(FunctionObject f | f.getACall() = r.getValue().getAFlowNode() |
20-
f.neverReturns()
21-
) and
22-
not exists(Attribute meth | meth = ((Call)r.getValue()).getFunc() | meth.getName() = "__init__")
16+
where
17+
exists(Function init | init.isInitMethod() and r.getScope() = init and exists(r.getValue())) and
18+
not r.getValue() instanceof None and
19+
not exists(FunctionValue f | f.getACall() = r.getValue().getAFlowNode() | f.neverReturns()) and
20+
// to avoid double reporting, don't trigger if returning result from other __init__ function
21+
not exists(Attribute meth | meth = r.getValue().(Call).getFunc() | meth.getName() = "__init__")
2322
select r, "Explicit return in __init__ method."

python/ql/test/query-tests/Functions/general/explicit_return_in_init.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ class InitCallsInit(InitCallsError):
3737

3838
def __init__(self):
3939
return super(InitCallsInit, self).__init__()
40+
41+
# This is not ok, but we will only report root cause (ExplicitReturnInInit)
42+
class InitCallsBadInit(ExplicitReturnInInit):
43+
44+
def __init__(self):
45+
return ExplicitReturnInInit.__init__(self)

0 commit comments

Comments
 (0)