-
Notifications
You must be signed in to change notification settings - Fork 31
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.