Skip to content

SONARPY-2865: Create rule S7508: Unnecessary <list/reversed/set/sorted/tuple> call within <list/set/sorted/tuple>() #5045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rules/S7508/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
23 changes: 23 additions & 0 deletions rules/S7508/python/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"title": "Redundant collection functions should be avoided",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
],
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-7508",
"sqKey": "S7508",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "targeted",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW"
},
"attribute": "CONVENTIONAL"
}
}
60 changes: 60 additions & 0 deletions rules/S7508/python/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
This rule raises an issue when the functions `list()`, `tuple()`, `set()`, `sorted()`, or `reversed()` are unnecessarily wrapped around each other's return values or used to convert values that don't require conversion.

== Why is this an issue?

Python's built-in functions for processing iterables such as `list()`, `tuple()`, `set()`, `sorted()`, and `reversed()` are designed to accept any iterable as input. When these functions are unnecessarily nested within each other, it creates redundant operations that add unnecessary computational overhead by creating intermediate data structures, decrease code readability and make the intention less clear, and waste memory by duplicating data structures temporarily.


== How to fix it

When the outer function is given a collection but could have been given an iterable, the unnecessary conversion should be removed. For example, in `sorted(list(iterable))`, the outer `sorted()` function can accept an iterable directly, so the inner `list()` call is redundant and should be removed.

When the function `sorted()` is wrapped with `list()`, remove this conversion operation, since `sorted()` already returns a list.

=== Code examples

==== Noncompliant code example

[source,python,diff-id=1,diff-type=noncompliant]
----
iterable = (3, 1, 4, 1)

sorted_of_list = list(sorted(iterable)) # Noncompliant
----

==== Compliant solution

[source,python,diff-id=1,diff-type=compliant]
----
iterable = (3, 1, 4, 1)

sorted_of_list = sorted(iterable)
----

== Resources
=== Documentation
* Python Documentation - https://docs.python.org/3/library/stdtypes.html#list[list]
* Python Documentation - https://docs.python.org/3/library/stdtypes.html#tuple[tuple]
* Python Documentation - https://docs.python.org/3/library/stdtypes.html#set[set]
* Python Documentation - https://docs.python.org/3/library/functions.html#sorted[sorted]
* Python Documentation - https://docs.python.org/3/library/functions.html#reversed[reversed]

ifdef::env-github,rspecator-view[]

== Implementation details

The rule specified by SONARPY-2877 specifies the special case of `reverse(sorted(...))` and `sorted(reverse(...))`. Because of this, this rule cannot raise in these cases to avoid raising twice for the same issue.


=== Highlight

The function call which should be removed should be highlighted.

=== Message

Remove this redundant cast.

=== Quickfix


endif::env-github,rspecator-view[]