rspec/rules/S5781/rule.adoc

43 lines
1.2 KiB
Plaintext
Raw Normal View History

2023-08-03 09:54:15 +02:00
This rule raises an issue when the same value is used multiple times when instantiating a set literal.
== Why is this an issue?
2023-08-03 09:54:15 +02:00
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.
2020-06-30 12:50:28 +02:00
2023-08-03 09:54:15 +02:00
Creating a set with redundant elements is prone to errors and confusion. A duplicated value in a set literal should be either:
2021-02-02 15:02:10 +01:00
2023-08-03 09:54:15 +02:00
* 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.
2020-06-30 12:50:28 +02:00
2023-08-03 09:54:15 +02:00
=== Code examples
2020-06-30 12:50:28 +02:00
2023-08-03 09:54:15 +02:00
==== Noncompliant code example
2020-06-30 12:50:28 +02:00
2023-08-03 09:54:15 +02:00
[source,text,diff-id=1,diff-type=noncompliant]
2020-06-30 12:50:28 +02:00
----
2023-08-03 09:54:15 +02:00
{"one", "two", "one"} # Noncompliant: the value "one" is duplicated.
2020-06-30 12:50:28 +02:00
def func(a1, a2, a3):
2023-08-03 09:54:15 +02:00
{a1, a2, a1} # Noncompliant: the value a1 is duplicated.
2020-06-30 12:50:28 +02:00
----
2023-08-03 09:54:15 +02:00
==== Compliant solution
2020-06-30 12:50:28 +02:00
2023-08-03 09:54:15 +02:00
[source,text,diff-id=1,diff-type=compliant]
2020-06-30 12:50:28 +02:00
----
{"one", "two", "three"}
def func(a1, a2, a3):
{a1, a2, a3}
----
== Resources
2020-06-30 12:50:28 +02:00
2023-08-03 09:54:15 +02:00
=== Documentation
* Python Documentation - https://docs.python.org/3/reference/expressions.html#set-displays[Set displays]
2020-06-30 12:50:28 +02:00