Skip to content

Commit 850c001

Browse files
Update rule metadata for v2.7 (#619)
1 parent 7c771d9 commit 850c001

File tree

13 files changed

+60
-47
lines changed

13 files changed

+60
-47
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ <h2>Noncompliant Code Example</h2>
1313
<pre>
1414
def foo():
1515
try:
16-
raise FileExistsError()
17-
except (OSError, RuntimeError) as e:
16+
raise FloatingPointError()
17+
except (ArithmeticError, RuntimeError) as e:
1818
print(e)
19-
except FileExistsError as e: # Noncompliant. FileExistsError is a subclass of OSError
19+
except FloatingPointError as e: # Noncompliant. FloatingPointError is a subclass of ArithmeticError
2020
print("Never executed")
21-
except FileNotFoundError as e: # Noncompliant. FileNotFoundError is a subclass of OSError
21+
except OverflowError as e: # Noncompliant. OverflowError is a subclass of ArithmeticError
2222
print("Never executed")
2323

2424
try:
@@ -39,12 +39,12 @@ <h2>Compliant Solution</h2>
3939
<pre>
4040
def foo():
4141
try:
42-
raise FileExistsError()
43-
except FileExistsError as e:
44-
print("Never executed")
45-
except FileNotFoundError as e:
46-
print("Never executed")
47-
except (OSError, RuntimeError) as e:
42+
raise FloatingPointError()
43+
except FloatingPointError as e:
44+
print("Executed")
45+
except OverflowError as e:
46+
print("Executed")
47+
except (ArithmeticError, RuntimeError) as e:
4848
print(e)
4949

5050
try:

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<p>Using <code>return</code>, <code>break</code> or <code>continue</code> in a <code>finally</code> block suppresses the propagation of any unhandled
2-
exception which was raised in the <code>try</code>, <code>else</code> or <code>except</code> blocks.</p>
2+
exception which was raised in the <code>try</code>, <code>else</code> or <code>except</code> blocks. It will also ignore their return statements.</p>
3+
<p><code><a href="https://docs.python.org/3/library/exceptions.html#SystemExit">SystemExit</a></code> is raised when <code>sys.exit()</code> is
4+
called. <code><a href="https://docs.python.org/3/library/exceptions.html#KeyboardInterrupt">KeyboardInterrupt</a></code> is raised when the user asks
5+
the program to stop by pressing interrupt keys. Both exceptions are expected to propagate up until the application stops. It is ok to catch them when
6+
a clean-up is necessary but they should be raised again immediately. They should never be ignored.</p>
7+
<p>If you need to ignore every other exception you can simply catch the <code>Exception</code> class. However you should be very careful when you do
8+
this as it will ignore other important exceptions such as <code><a
9+
href="https://docs.python.org/3/library/exceptions.html#MemoryError">MemoryError</a></code></p>
10+
<p>In python 2 it is possible to raise old style classes. You can use a bare <code>except:</code> statement to catch every exception. Remember to
11+
still reraise <code>SystemExit</code> and <code>KeyboardInterrupt</code>.</p>
312
<p>This rule raises an issue when a jump statement (<code>break</code>, <code>continue</code>, <code>return</code>) would force the control flow to
413
leave a finally block.</p>
514
<h2>Noncompliant Code Example</h2>
@@ -39,6 +48,14 @@ <h2>Noncompliant Code Example</h2>
3948
raise
4049
finally:
4150
break # This will prevent SystemExit from raising
51+
52+
def continue_whatever_happens_noncompliant():
53+
for i in range(10):
54+
try:
55+
raise ValueError()
56+
finally:
57+
continue # Noncompliant
58+
4259
</pre>
4360
<h2>Compliant Solution</h2>
4461
<pre>
@@ -75,6 +92,15 @@ <h2>Compliant Solution</h2>
7592
except (SystemExit) as e:
7693
print("Exiting")
7794
raise # SystemExit is re-raised
95+
96+
import logging
97+
98+
def continue_whatever_happens_compliant():
99+
for i in range(10):
100+
try:
101+
raise ValueError()
102+
except Exception:
103+
logging.exception("Failed") # Ignore all "Exception" subclasses yet allow SystemExit and other important exceptions to pass
78104
</pre>
79105
<h2>See</h2>
80106
<ul>

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
<p>In some cases a comparison with operators <code>==</code>, or <code>!=</code> will always return True or always return False. When this happens,
22
the comparison and all its dependent code can simply be removed. This includes:</p>
33
<ul>
4-
<li> comparing <code>None</code> with a value which will always be <code>None</code>. </li>
54
<li> comparing unrelated builtin types such as string and integer. </li>
6-
<li> comparing class instances which do not implement <code>__eq__</code>, or <code>__ne__</code> if <code>!=</code> is used, to an object of a
7-
different type (builtin or from an unrelated class which also doesn't implement <code>__eq__</code> or <code>__ne__</code>). </li>
5+
<li> comparing class instances which do not implement <code>__eq__</code> or <code>__ne__</code> to an object of a different type (builtin or from
6+
an unrelated class which also doesn't implement <code>__eq__</code> or <code>__ne__</code>). </li>
87
</ul>
98
<h2>Noncompliant Code Example</h2>
109
<pre>
11-
mynone = None
12-
13-
foo = mynone == None # Noncompliant. Always True.
14-
1510
foo = 1 == "1" # Noncompliant. Always False.
1611

1712
foo = 1 != "1" # Noncompliant. Always True.
@@ -22,14 +17,6 @@ <h2>Noncompliant Code Example</h2>
2217
myvar = A() == 1 # Noncompliant. Always False.
2318
myvar = A() != 1 # Noncompliant. Always True.
2419

25-
class Ne:
26-
def __ne__(self, other):
27-
return True
28-
29-
myvar = Ne() == 1 # Noncompliant. Always False. "__eq__" does not call "__ne__" by default
30-
myvar = 1 == Ne() # Noncompliant. Always False.
31-
myvar = Ne() != 1 # Ok
32-
myvar = 1 != Ne() # Ok
3320
</pre>
3421
<h2>Compliant Solution</h2>
3522
<pre>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"tags": [
1010
"unused"
1111
],
12-
"defaultSeverity": "Major",
12+
"defaultSeverity": "Blocker",
1313
"ruleSpecification": "RSPEC-2159",
1414
"sqKey": "S2159",
1515
"scope": "Main"

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@ <h2>Noncompliant Code Example</h2>
77
mystring = "1"
88
value = myint is mystring # Noncompliant. Always False
99
value = myint is not mystring # Noncompliant. Always True
10-
11-
if (myint is None): # Noncompliant. Always False
12-
pass
1310
</pre>
1411

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"tags": [
1010

1111
],
12-
"defaultSeverity": "Major",
12+
"defaultSeverity": "Blocker",
1313
"ruleSpecification": "RSPEC-3403",
1414
"sqKey": "S3403",
1515
"scope": "Main"

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ <h2>Noncompliant Code Example</h2>
1010
result = 0
1111
try:
1212
print("foo")
13+
except ValueError as e:
14+
pass
1315
else:
1416
if param:
1517
raise ValueError()
@@ -26,6 +28,8 @@ <h2>Compliant Solution</h2>
2628
result = 0
2729
try:
2830
print("foo")
31+
except ValueError as e:
32+
pass
2933
else:
3034
if param:
3135
raise ValueError()

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"type": "CODE_SMELL",
44
"status": "ready",
55
"tags": [
6-
6+
"error-handling",
7+
"unpredictable",
8+
"confusing"
79
],
810
"defaultSeverity": "Critical",
911
"ruleSpecification": "RSPEC-5704",

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
<p>The special method <a
22
href="https://docs.python.org/3/reference/datamodel.html?highlight=__exit__%20special#object.__exit__"><code>__exit__</code></a> should only raise an
3-
exception when it fails. I should never raise the provided exception, it is the caller's responsibility.</p>
4-
<p>Raising this exception will make the stack trace difficult to understand.</p>
3+
exception when it fails. It should never raise the provided exception, it is the caller's responsibility.</p>
4+
<p> Raising this exception will make the stack trace difficult to understand.</p>
55
<p>The <code>__exit__</code> method can filter passed-in exceptions by simply returning True or False.</p>
66
<p>This rule raises an issue when:</p>
7-
<ul>
8-
<li> an <code>__exit__</code> method has a bare <code>raise</code> outside of an <code>except</code> block. </li>
9-
<li> an <code>__exit__</code> method raises the exception provided as parameter. </li>
10-
</ul>
7+
<p> * an <code>__exit__</code> method has a bare <code>raise</code> outside of an <code>except</code> block.</p>
8+
<p> * an <code>__exit__</code> method raises the exception provided as parameter.</p>
119
<h2>Noncompliant Code Example</h2>
1210
<pre>
1311
class MyContextManager:
@@ -44,9 +42,7 @@ <h2>Compliant Solution</h2>
4442
raise MemoryError("No more memory") # This is ok too.
4543
</pre>
4644
<h2>See</h2>
47-
<ul>
48-
<li> Python documentation <del></del> <a href="https://docs.python.org/3/reference/datamodel.html?highlight=__exit__%20special#object.__exit__">The
49-
<code>__exit__</code> special method</a> </li>
50-
<li> PEP 343 <del></del> <a href="https://www.python.org/dev/peps/pep-0343/">The "with" Statement</a> </li>
51-
</ul>
45+
<p> * Python documentation – <a href="https://docs.python.org/3/reference/datamodel.html?highlight=__exit__%20special#object.__exit__">The
46+
<code>__exit__</code> special method</a></p>
47+
<p> * PEP 343 – <a href="https://www.python.org/dev/peps/pep-0343/">The "with" Statement</a></p>
5248

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"type": "CODE_SMELL",
44
"status": "ready",
55
"tags": [
6-
6+
"error-handling",
7+
"bad-practice"
78
],
89
"defaultSeverity": "Major",
910
"ruleSpecification": "RSPEC-5706",

0 commit comments

Comments
 (0)