rspec/rules/S5781/rule.adoc
2023-08-03 09:54:15 +02:00

43 lines
1.2 KiB
Plaintext

This rule raises an issue when the same value is used multiple times when instantiating a set literal.
== Why is this an issue?
By definition, a set cannot hold the same value multiple times.
When instantiating a set literal with the same value repeated multiple times,
only the last occurrence of the duplicated value will remain.
Creating a set with redundant elements is prone to errors and confusion. A duplicated value in a set literal should be either:
* modified, as it was mistakenly put in the set instead an actual value which would lead to bugs and errors further in the program.
* removed, as it was a simple duplication, making the code confusing and difficult to maintain.
=== Code examples
==== Noncompliant code example
[source,text,diff-id=1,diff-type=noncompliant]
----
{"one", "two", "one"} # Noncompliant: the value "one" is duplicated.
def func(a1, a2, a3):
{a1, a2, a1} # Noncompliant: the value a1 is duplicated.
----
==== Compliant solution
[source,text,diff-id=1,diff-type=compliant]
----
{"one", "two", "three"}
def func(a1, a2, a3):
{a1, a2, a3}
----
== Resources
=== Documentation
* Python Documentation - https://docs.python.org/3/reference/expressions.html#set-displays[Set displays]