-
Notifications
You must be signed in to change notification settings - Fork 30
SONARPY-2870: Create rule S7492: List comprehension in <any/all>() prevents short-circuiting #5013
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
base: master
Are you sure you want to change the base?
Conversation
286bc00
to
5499fcc
Compare
5499fcc
to
546ce60
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks quite good! I would just reorder things a bit.
rules/S7492/python/rule.adoc
Outdated
res_all = all([x > 2 for x in numbers]) # Noncompliant | ||
res_any = any([x > 2 for x in numbers]) # Noncompliant |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I think it is a good idea to say what is the issue. Also I would keep the example to only 1 use case either any
or all
. It is just an example so there is no need to be exhaustive.
rules/S7492/python/rule.adoc
Outdated
---- | ||
numbers = [1, 5, 0, 10] | ||
|
||
res_all = all(x > 2 for x in numbers) # Compliant: Stops at first False (0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here on the other hand I would remove the commentary about when the iteration stops, as it can be read as: it is compliant as it stop on the first False, instead of it is compliant because it allows for the short-circuiting behavior to take place.
rules/S7492/python/rule.adoc
Outdated
* With `any(...)`, evaluation stops as soon as a truthy value is encountered | ||
* With `all(...)`, evaluation stops as soon as a falsy value is encountered | ||
|
||
This approach saves both processing time and memory, especially for large iterables or when the condition has side effects or is computationally expensive. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel this sentence should be part of Why is it an issue
because you do not state why it is a problem that the short-circuiting mechanism is not available when using a list. So I would suggest move this sentence and phrase it in the way that it could be a memory and processing time loss. We could then think of removing that section if it does not make sense anymore, I leave it up to you.
ac35dc7
to
840644a
Compare
840644a
to
adbe981
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a small comment on the resources otherwise looks good to me.
rules/S7492/python/rule.adoc
Outdated
== Resources | ||
=== Documentation | ||
* Python Wiki - https://wiki.python.org/moin/Generators[Generators] | ||
* Python Doc - https://docs.python.org/3/glossary.html#term-generator[Generator Glossary Entry] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not catch this one during the first path but it should be Python Documentation
|
|
You can preview this rule here (updated a few minutes after each push).
Review
A dedicated reviewer checked the rule description successfully for: