File tree Expand file tree Collapse file tree
test/query-tests/Statements/ReturnOrYieldOutsideOfFunction Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # invalid class with return outside of a function
2+ class InvalidClass1 (object ):
3+ if [1 , 2 , 3 ]:
4+ return "Exists"
5+
6+ # invalid statement with yield outside of a function
7+ for i in [1 , 2 , 3 ]:
8+ yield i
Original file line number Diff line number Diff line change 1+ <!DOCTYPE qhelp PUBLIC
2+ "-//Semmle//qhelp//EN"
3+ "qhelp.dtd">
4+ <qhelp >
5+
6+ <overview >
7+ <p >In Python, <code >return</code > and <code >yield</code > statements as well as <code >yield from</code >
8+ expression can only be used within a function. Having them placed outside of a function or a class
9+ method will raise a <code >SyntaxError</code > at runtime.</p >
10+ </overview >
11+
12+ <recommendation >
13+ <p >The presence of <code >return</code > and <code >yield</code > statements or <code >yield from</code >
14+ expression outside of a function or a class method suggests a logical error, so it is not possible
15+ to suggest a general fix.</p >
16+ </recommendation >
17+
18+ <example >
19+ <p >In this example, a <code >return</code > statement is used outside of a class method in a class and
20+ a <code >yield</code > statement is used outside of a function in a scope of a module.</p >
21+ <sample src =" ReturnOrYieldOutsideOfFunction.py" />
22+ </example >
23+
24+ <references >
25+ <li >Python reference: <a href =" https://docs.python.org/3.7/reference/simple_stmts.html#the-return-statement" >
26+ The return statement</a >.</li >
27+ <li >Python reference: <a href =" https://docs.python.org/3.7/reference/simple_stmts.html#yield" >
28+ The yield statement</a >.</li >
29+ <li >Python PEP-380: <a href =" https://docs.python.org/3/whatsnew/3.3.html#pep-380" >
30+ The yield from expression</a >.</li >
31+ </references >
32+ </qhelp >
Original file line number Diff line number Diff line change 1+ /**
2+ * @name return or yield are used outside of a function
3+ * @description return and yield statements should be used only within a function.
4+ * @kind problem
5+ * @tags reliability
6+ * correctness
7+ * @problem.severity error
8+ * @sub-severity low
9+ * @precision very-high
10+ * @id py/return-or-yield-outside-of-function
11+ */
12+
13+ import python
14+
15+ from AstNode node , string kind
16+ where
17+ not node .getScope ( ) instanceof Function and
18+ (
19+ node instanceof Return and kind = "return"
20+ or
21+ node instanceof Yield and kind = "yield"
22+ or
23+ node instanceof YieldFrom and kind = "yield from"
24+ )
25+ select node , "'" + kind + "' is used outside of a function."
Original file line number Diff line number Diff line change 1+ | ReturnOrYieldOutsideOfFunction_test.py:31:9:31:23 | Return | 'return' is used outside of a function. |
2+ | ReturnOrYieldOutsideOfFunction_test.py:36:9:36:15 | Yield | 'yield' is used outside of a function. |
3+ | ReturnOrYieldOutsideOfFunction_test.py:41:9:41:25 | YieldFrom | 'yield from' is used outside of a function. |
4+ | ReturnOrYieldOutsideOfFunction_test.py:45:5:45:12 | Return | 'return' is used outside of a function. |
5+ | ReturnOrYieldOutsideOfFunction_test.py:49:5:49:11 | Yield | 'yield' is used outside of a function. |
6+ | ReturnOrYieldOutsideOfFunction_test.py:53:5:53:16 | YieldFrom | 'yield from' is used outside of a function. |
7+ | ReturnOrYieldOutsideOfFunction_test.py:57:1:57:14 | YieldFrom | 'yield from' is used outside of a function. |
8+ | ReturnOrYieldOutsideOfFunction_test.py:60:1:60:12 | Return | 'return' is used outside of a function. |
9+ | ReturnOrYieldOutsideOfFunction_test.py:63:1:63:11 | Yield | 'yield' is used outside of a function. |
Original file line number Diff line number Diff line change 1+ Statements/ReturnOrYieldOutsideOfFunction.ql
Original file line number Diff line number Diff line change 1+ # valid class with return inside a function
2+ class ValidClass1 (object ):
3+ def class_method (self ):
4+ return False
5+
6+ # valid class with yield inside a function
7+ class ValidClass2 (object ):
8+ def class_method (self ):
9+ yield 1
10+
11+ # valid class with yield from inside a function
12+ class ValidClass3 (object ):
13+ def class_method (self ):
14+ yield from [1 , 2 ]
15+
16+ # valid function with the return statement
17+ def valid_func1 ():
18+ return True
19+
20+ # valid function with the yield statement
21+ def valid_func2 ():
22+ yield 1
23+
24+ # valid function with the yield from statement
25+ def valid_func3 ():
26+ yield from [1 , 2 ]
27+
28+ # invalid class with return outside of a function
29+ class InvalidClass1 (object ):
30+ if [1 , 2 , 3 ]:
31+ return "Exists"
32+
33+ # invalid class with yield outside of a function
34+ class InvalidClass2 (object ):
35+ while True :
36+ yield 1
37+
38+ # invalid class with yield from outside of a function
39+ class InvalidClass2 (object ):
40+ while True :
41+ yield from [1 , 2 ]
42+
43+ # invalid statement with return outside of a function
44+ for i in [1 , 2 , 3 ]:
45+ return i
46+
47+ # invalid statement with yield outside of a function
48+ for i in [1 , 2 , 3 ]:
49+ yield i
50+
51+ # invalid statement with yield from outside of a function
52+ for i in [1 , 2 , 3 ]:
53+ yield from i
54+
55+ # invalid statement with yield from outside of a function
56+ var = [1 ,2 ,3 ]
57+ yield from var
58+
59+ # invalid statement with return outside of a function
60+ return False
61+
62+ # invalid statement with yield outside of a function
63+ yield False
You can’t perform that action at this time.
0 commit comments