Skip to content

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented May 7, 2025

You can preview this rule here (updated a few minutes after each push).

Review

A dedicated reviewer checked the rule description successfully for:

  • logical errors and incorrect information
  • information gaps and missing content
  • text style and tone
  • PR summary and labels follow the guidelines

@github-actions github-actions bot added the python label May 7, 2025
@Seppli11 Seppli11 force-pushed the rule/add-RSPEC-S7492 branch 2 times, most recently from 286bc00 to 5499fcc Compare May 7, 2025 14:31
@Seppli11 Seppli11 force-pushed the rule/add-RSPEC-S7492 branch from 5499fcc to 546ce60 Compare May 7, 2025 14:33
@Seppli11 Seppli11 requested a review from Copilot May 7, 2025 14:35
Copilot

This comment was marked as resolved.

Copy link
Contributor

@joke1196 joke1196 left a 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.

Comment on lines 21 to 22
res_all = all([x > 2 for x in numbers]) # Noncompliant
res_any = any([x > 2 for x in numbers]) # Noncompliant
Copy link
Contributor

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.

----
numbers = [1, 5, 0, 10]

res_all = all(x > 2 for x in numbers) # Compliant: Stops at first False (0)
Copy link
Contributor

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.

* 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.
Copy link
Contributor

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.

@Seppli11 Seppli11 changed the title Create rule S7492 Create rule S7492: Unnecessary list comprehension in <any/all>() prevents short-circuiting May 8, 2025
@Seppli11 Seppli11 changed the title Create rule S7492: Unnecessary list comprehension in <any/all>() prevents short-circuiting Create rule S7492: List comprehension in <any/all>() prevents short-circuiting May 8, 2025
@Seppli11 Seppli11 force-pushed the rule/add-RSPEC-S7492 branch from ac35dc7 to 840644a Compare May 8, 2025 09:58
@Seppli11 Seppli11 force-pushed the rule/add-RSPEC-S7492 branch from 840644a to adbe981 Compare May 8, 2025 09:59
@Seppli11 Seppli11 requested a review from joke1196 May 8, 2025 10:00
Copy link
Contributor

@joke1196 joke1196 left a 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.

== 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]
Copy link
Contributor

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

@Seppli11 Seppli11 changed the title Create rule S7492: List comprehension in <any/all>() prevents short-circuiting SONARPY-2870: Create rule S7492: List comprehension in <any/all>() prevents short-circuiting May 9, 2025
Copy link

sonarqube-next bot commented May 9, 2025

Copy link

sonarqube-next bot commented May 9, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants