diff --git a/rules/S7508/metadata.json b/rules/S7508/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S7508/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S7508/python/metadata.json b/rules/S7508/python/metadata.json new file mode 100644 index 00000000000..1b897a38663 --- /dev/null +++ b/rules/S7508/python/metadata.json @@ -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" + } +} diff --git a/rules/S7508/python/rule.adoc b/rules/S7508/python/rule.adoc new file mode 100644 index 00000000000..bb1c989f029 --- /dev/null +++ b/rules/S7508/python/rule.adoc @@ -0,0 +1,61 @@ +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[] \ No newline at end of file