Add GCI109 rule - Avoid using exceptions for control flow#108
Conversation
There was a problem hiding this comment.
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.
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 |
|
Hi @hassineabd, Could you please update your pom.xml to use this 2.6.0 version ? |
|
Hi @hassineabd, after testing your PR, I found some different minor problems (TI ko, not same test file between TU and TI,) that I corrected quickly locally. Thank you for your work !!! |
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