
Co-authored-by: Marco Borgeaud <89914223+marco-antognini-sonarsource@users.noreply.github.com>
99 lines
2.7 KiB
Plaintext
99 lines
2.7 KiB
Plaintext
This rule raises an issue when a `break` or `continue` statement is used outside of a loop.
|
|
|
|
== Why is this an issue?
|
|
|
|
`break` and `continue` are control flow statements used inside of loops. `break` is used to break out of its innermost enclosing loop and `continue` will continue with the next iteration.
|
|
|
|
The example below illustrates the use of `break` in a `while` loop:
|
|
|
|
[source,python]
|
|
----
|
|
n = 1
|
|
while n < 10:
|
|
if n % 3 == 0:
|
|
print("Found a number divisible by 3", n)
|
|
break
|
|
n = n + 1
|
|
----
|
|
|
|
This next example uses `continue` inside a `for` loop:
|
|
|
|
[source,python]
|
|
----
|
|
words = ["alice", "bob", "charlie"]
|
|
for word in words:
|
|
if word == word[::-1]:
|
|
print("Found a palindrome", word)
|
|
continue
|
|
print("This is not a palindrome", word)
|
|
----
|
|
|
|
Python will raise a `SyntaxError` when `break` or `continue` are used outside of `for` or `while` loops.
|
|
|
|
If the goal is to interrupt the main program flow, `quit()`, `exit()`, ``++os._exit()++`` and `sys.exit()` are the preferred way.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,python,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
narg=len(sys.argv)
|
|
if narg == 1:
|
|
print('@Usage: input_filename nelements nintervals')
|
|
break
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,python,diff-id=1,diff-type=compliant]
|
|
----
|
|
narg=len(sys.argv)
|
|
if narg == 1:
|
|
print('@Usage: input_filename nelements nintervals')
|
|
sys.exit()
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops[break and continue Statements]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Remove this "xxx" statement
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== is related to: S910
|
|
|
|
=== on 4 Nov 2014, 15:59:14 Ann Campbell wrote:
|
|
pylint:E0103
|
|
|
|
=== on 9 May 2016, 15:41:47 Evgeny Mandrikov wrote:
|
|
I don't know any C/{cpp} compiler, which will allow to use "continue" outside of loop and "break" outside of switch and loop, hence not applicable.
|
|
|
|
=== on 9 May 2016, 15:44:26 Evgeny Mandrikov wrote:
|
|
Note that PC-Lint error codes from 1 to 199 for C and from 1001 to 1199 for {cpp} are syntax errors.
|
|
|
|
=== on 16 May 2016, 17:06:00 Ann Campbell wrote:
|
|
\[~evgeny.mandrikov] I guess this means that PC-Lint doesn't expect compilable code?
|
|
|
|
=== on 16 May 2016, 17:14:19 Evgeny Mandrikov wrote:
|
|
\[~ann.campbell.2] I suppose that it expects for proper analysis in general, but when this is not the case it generates more precise "parse error".
|
|
|
|
=== on 16 May 2016, 17:50:32 Ann Campbell wrote:
|
|
For the record PC-Lint rules in these ranges are Syntax errors: 1-199, 1001-1199.
|
|
|
|
endif::env-github,rspecator-view[]
|