All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.py.rules.python.S5781.html Maven / Gradle / Ivy

The newest version!

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

{"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

{"one", "two", "three"}

def func(a1, a2, a3):
    {a1, a2, a3}

Resources

Documentation





© 2015 - 2025 Weber Informatics LLC | Privacy Policy