GCI108 PreferAppendLeft #Python #DLG #Build#70
GCI108 PreferAppendLeft #Python #DLG #Build#70dedece35 merged 6 commits intogreen-code-initiative:mainfrom
Conversation
Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
|
This PR has been automatically marked as stale because it has no activity for 30 days. |
There was a problem hiding this comment.
Pull Request Overview
This PR implements a new static analysis rule GCI97 "PreferAppendLeft" that detects inefficient list operations and suggests using deque.appendleft() instead of list.insert(0, value) for better performance when inserting at the beginning of collections.
- Adds rule implementation that detects calls to
insert(0, value)on any object - Creates comprehensive test cases covering various scenarios including edge cases like floating point zeros
- Updates the rule repository and changelog to include the new rule
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/main/java/org/greencodeinitiative/creedengo/python/checks/PreferAppendLeft.java | Core rule implementation that detects insert(0, value) patterns |
| src/test/java/org/greencodeinitiative/creedengo/python/checks/PreferAppendLeftTest.java | Unit test class for the new rule |
| src/test/resources/checks/preferAppendLeft.py | Test data file with compliant and non-compliant code examples |
| src/main/java/org/greencodeinitiative/creedengo/python/PythonRuleRepository.java | Registration of the new rule in the rule repository |
| CHANGELOG.md | Documentation of the new rule addition |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| if (firstArg.is(NUMERIC_LITERAL)) { | ||
| if (isZeroLiteral(firstArg)) { | ||
| context.addIssue(callExpression, DESCRIPTION); | ||
| } |
There was a problem hiding this comment.
Potential ClassCastException if the first argument is not a RegularArgument. The code should check the argument type before casting or handle other argument types like KeywordArgument.
| } | |
| if (arguments.get(0) instanceof RegularArgument) { | |
| firstArg = ((RegularArgument) arguments.get(0)).expression(); | |
| if (firstArg.is(NUMERIC_LITERAL)) { | |
| if (isZeroLiteral(firstArg)) { | |
| context.addIssue(callExpression, DESCRIPTION); | |
| } | |
| } |
| if (expression.is(NUMERIC_LITERAL)) { | ||
| NumericLiteral numericLiteral = (NumericLiteral) expression; | ||
| String value = numericLiteral.valueAsString(); | ||
| return "0".equals(value) || "0.0".equals(value) || "0.00".equals(value); |
There was a problem hiding this comment.
The hardcoded string comparisons for zero values are incomplete and fragile. Consider using a more robust approach like parsing the numeric value and checking if it equals zero, which would handle cases like "0.000", "00", or "0e0".
| return "0".equals(value) || "0.0".equals(value) || "0.00".equals(value); | |
| try { | |
| return Double.parseDouble(value) == 0.0; | |
| } catch (NumberFormatException e) { | |
| // If parsing fails, it's not a valid zero literal | |
| return false; | |
| } |
src/test/java/org/greencodeinitiative/creedengo/python/checks/PreferAppendLeftTest.java
Show resolved
Hide resolved
| import static org.sonar.plugins.python.api.tree.Tree.Kind.*; | ||
|
|
||
|
|
||
| @Rule(key = "GCI97") |
…lot suggestion, add IT Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
Verified that the rule does not exist.
Confirmed that the rule is not listed in Rules.MD, so a new ID GCI97 was created.
Added a corresponding Python unit test.
Updated the CHANGELOG accordingly.