Skip to content

GCI108 PreferAppendLeft #Python #DLG #Build#70

Merged
dedece35 merged 6 commits intogreen-code-initiative:mainfrom
cleophass:GCI97-python
Aug 27, 2025
Merged

GCI108 PreferAppendLeft #Python #DLG #Build#70
dedece35 merged 6 commits intogreen-code-initiative:mainfrom
cleophass:GCI97-python

Conversation

@cleophass
Copy link
Copy Markdown
Contributor

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.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jul 3, 2025

This PR has been automatically marked as stale because it has no activity for 30 days.
Please add a comment if you want to keep the issue open. Thank you for your contributions!

@github-actions github-actions bot added the stale label Jul 3, 2025
@dedece35 dedece35 requested a review from Copilot August 21, 2025 21:44
Copy link
Copy Markdown
Contributor

Copilot AI left a 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 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);
}
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
}
if (arguments.get(0) instanceof RegularArgument) {
firstArg = ((RegularArgument) arguments.get(0)).expression();
if (firstArg.is(NUMERIC_LITERAL)) {
if (isZeroLiteral(firstArg)) {
context.addIssue(callExpression, DESCRIPTION);
}
}

Copilot uses AI. Check for mistakes.
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);
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

Suggested change
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;
}

Copilot uses AI. Check for mistakes.
import static org.sonar.plugins.python.api.tree.Tree.Kind.*;


@Rule(key = "GCI97")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please give the rigth rule id

@github-actions github-actions bot removed the stale label Aug 22, 2025
@cleophass cleophass changed the title GCI97 PreferAppendLeft #Python #DLG #Build GCI108 PreferAppendLeft #Python #DLG #Build Aug 26, 2025
…lot suggestion, add IT

Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
@dedece35 dedece35 merged commit a520fc0 into green-code-initiative:main Aug 27, 2025
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants