-
Notifications
You must be signed in to change notification settings - Fork 22
Add GCI109 rule - Avoid using exceptions for control flow #108
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: main
Are you sure you want to change the base?
Add GCI109 rule - Avoid using exceptions for control flow #108
Conversation
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.
Hi @hassineabd,
thank you a lot for this interesting PR ...
before accepting this PR, please, you must do following tasks :
- update CHANGELOG.md with your upgrade in the current repository
- follow process for a new rule : https://github.com/green-code-initiative/creedengo-common/blob/main/doc/starter-pack.md#implement-a-new-rule
Please, in the rule documentation in creedengo-rules-specifications, I will particularly vigilent about the proof of your ideas ... please feel free to giver some URLs to prove your idea. A full example : https://github.com/green-code-initiative/creedengo-rules-specifications/blob/main/src/main/rules/GCI107/python/GCI107.asciidoc
Please, also, take into account Copilot review, bottom, and adapt / complete your unit tests with new modifications.
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.
Pull Request Overview
This PR introduces the GCI109 rule to detect and discourage the use of exceptions for control flow in Python code, which is an anti-pattern that negatively impacts performance and energy consumption. The rule identifies try-except blocks that catch common control flow exceptions (KeyError, IndexError, AttributeError, StopIteration) and suggests using built-in alternatives like .get(), getattr(), bounds checking, or default parameters.
Key changes:
- Implemented
AvoidExceptionsForControlFlowCheckto detect control flow exception patterns - Added test coverage for both compliant and non-compliant code patterns
- Integrated the rule into the creedengo profile and rule repository
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
AvoidExceptionsForControlFlowCheck.java |
Core implementation of GCI109 rule with AST traversal and exception detection logic |
AvoidExceptionsForControlFlowCheckTest.java |
Unit test for the new rule |
avoidExceptionsForControlFlow.py (test resources) |
Test cases demonstrating non-compliant and compliant patterns |
avoidExceptionsForControlFlow.py (integration test) |
Integration test file with same test cases |
PythonRuleRepository.java |
Registration of the new check in the rule repository |
creedengo_way_profile.json |
Addition of GCI109 to the default profile |
GCIRulesIT.java |
Integration test validating rule detection on test file |
Comments suppressed due to low confidence (1)
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
...java/org/greencodeinitiative/creedengo/python/checks/AvoidExceptionsForControlFlowCheck.java
Show resolved
Hide resolved
|
Hi @hassineabd, thank you for last modifications but it lacks the part about "creedengo-specifications-rules" in my last comment. |
|
Hello @dedece35 |
This PR implements GCI109 rule to detect and reduce the use of exceptions for control flow in Python code.
implemetation details : this rule detects the use of common control flow exceptions:
KeyErrorIndexErrorAttributeErrorStopIteration