rspec/rules/S6466/python/rule.adoc
2023-08-04 17:10:05 +02:00

50 lines
1.0 KiB
Plaintext

This rule raises an issue when trying to access a list or a tuple index that is out of bounds.
== Why is this an issue?
Trying to access a list or a tuple index that is beyond the size of the list/tuple is probably a mistake and will result in an `IndexError`.
=== Code examples
==== Noncompliant code example
[source,python,diff-id=1,diff-type=noncompliant]
----
def fun():
ls = [1, 2, 3]
foo(ls[3]) # Noncompliant: the last index of the list ls is 2. Using the index 3 will raise an IndexError.
----
==== Compliant solution
[source,python,diff-id=1,diff-type=compliant]
----
def fun():
ls = [1, 2, 3]
foo(ls[2])
----
== Resources
=== Documentation
* Python Documentation - https://docs.python.org/3/library/exceptions.html#IndexError[IndexError]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Fix this access on a list element that may trigger an "IndexError".
Fix this access on a tuple element that may trigger an "IndexError".
'''
endif::env-github,rspecator-view[]