Skip to content

Commit 7ef0e84

Browse files
joke1196sonartech
authored andcommitted
SONARPY-3961 Update rule metadata (#998)
GitOrigin-RevId: 73734302e79b86b1ff98e06dcdbe905290b630e0
1 parent beca529 commit 7ef0e84

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed
Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1+
<p>This rule raises an issue when a <code>return</code> statement with a value is used inside a generator function (a function that uses
2+
<code>yield</code>).</p>
13
<h2>Why is this an issue?</h2>
2-
<p>Functions that use <code>yield</code> are known as "generators". Before Python 3.3, generators cannot <code>return</code> values. Similarly,
3-
functions that use <code>return</code> cannot use <code>yield</code>. Doing so will cause a <code>SyntaxError</code>.</p>
4-
<p>Either upgrade your version of Python to a version &gt;= 3.3, or don’t use both return and yield in a function.</p>
5-
<h3>Noncompliant code example</h3>
6-
<pre>
7-
def adder(n):
8-
num = 0
9-
while num &lt; n:
10-
yield num
11-
num += 1
12-
return num #Noncompliant
4+
<p>When a generator function uses <code>return</code> with a value, the value is not sent to the caller like a normal function return. Instead, it is
5+
silently attached to the <code>StopIteration</code> exception’s <code>value</code> attribute. Most callers iterate generators with a <code>for</code>
6+
loop, which swallows <code>StopIteration</code> automatically and discards the returned value entirely. This makes the <code>return</code> value
7+
effectively invisible and can lead to subtle bugs where developers expect the value to be accessible but it never reaches the calling code.</p>
8+
<p>A bare <code>return</code> (without a value) in a generator is fine — it simply ends the generator’s execution early, similar to how
9+
<code>return</code> works in a regular function.</p>
10+
<h2>How to fix it</h2>
11+
<h3>Code examples</h3>
12+
<h4>Noncompliant code example</h4>
13+
<pre data-diff-id="1" data-diff-type="noncompliant">
14+
def counter(n):
15+
num = 0
16+
while num &lt; n:
17+
yield num
18+
num += 1
19+
return num # Noncompliant: return with a value in a generator
1320
</pre>
21+
<h4>Compliant solution</h4>
22+
<pre data-diff-id="1" data-diff-type="compliant">
23+
def counter(n):
24+
num = 0
25+
while num &lt; n:
26+
yield num
27+
num += 1
28+
return # Compliant: bare return for early exit is fine
29+
</pre>
30+
<h2>Resources</h2>
31+
<h3>Documentation</h3>
32+
<ul>
33+
<li><a href="https://peps.python.org/pep-0380/">PEP 380 - Syntax for Delegating to a Subgenerator</a> - introduces <code>return</code> in
34+
generators</li>
35+
<li><a href="https://docs.python.org/3/reference/simple_stmts.html#the-return-statement">Python Documentation - The return statement</a></li>
36+
</ul>
1437

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S2712.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"title": "\"return\" and \"yield\" should not be used in the same function",
2+
"title": "\"return\" with a value should not be used in a generator function",
33
"type": "BUG",
44
"code": {
55
"impacts": {

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S8392.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,5 @@ <h3>Documentation</h3>
124124
<h3>Standards</h3>
125125
<ul>
126126
<li>CWE 668 - <a href="https://cwe.mitre.org/data/definitions/668.html">Exposure of Resource to Wrong Sphere</a></li>
127-
<li>CIS 6.6.1 - <a href="https://www.cisecurity.org/controls/">CIS Controls - Network Security</a></li>
128127
</ul>
129128

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S8392.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
"securityStandards": {
2929
"CWE": [
3030
668
31-
],
32-
"CIS": [
33-
"6.6.1"
3431
]
3532
}
3633
}

sonarpedia.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"languages": [
44
"PY"
55
],
6-
"latest-update": "2026-03-10T13:34:05.699012606Z",
6+
"latest-update": "2026-03-23T12:21:48.201596145Z",
77
"options": {
88
"no-language-in-filenames": true,
99
"preserve-filenames": true

0 commit comments

Comments
 (0)