Skip to content

[GCI27] System.arraycopy cannot always be used#193

Open
MohamedAliSidibe wants to merge 2 commits into
green-code-initiative:mainfrom
MohamedAliSidibe:edit_GCI27_about_system_arraycopy
Open

[GCI27] System.arraycopy cannot always be used#193
MohamedAliSidibe wants to merge 2 commits into
green-code-initiative:mainfrom
MohamedAliSidibe:edit_GCI27_about_system_arraycopy

Conversation

@MohamedAliSidibe
Copy link
Copy Markdown

Description

This PR fixes false positives in rule GCI27 (Use System.arraycopy to copy arrays).

Previously, the rule analyzed nested assignments inside conditional and try/catch/finally blocks, which caused incorrect suggestions of System.arraycopy(...) for code performing filtering or conditional logic.

Example of previous false positive:

for (boolean b : src) {
    if (b) {
        dest[++i] = b;
    }
}

This pattern cannot be safely replaced by System.arraycopy because it performs conditional filtering instead of a direct array copy.

Changes

  • Removed recursive extraction of nested assignments inside:

    • if
    • try
    • catch
    • finally
  • The rule now analyzes only direct assignments located in the main loop block.

Result

The rule still detects valid direct array copy patterns such as:

for (int i = 0; i < src.length; i++) {
    dest[i] = src[i];
}

while avoiding false positives for conditional or transformed copies.

@MohamedAliSidibe
Copy link
Copy Markdown
Author

Closes #19

@MohamedAliSidibe
Copy link
Copy Markdown
Author

MohamedAliSidibe commented May 20, 2026

Mahamadou Ali Sidibe / LesShreks des dépendances moisies / CGI

Copy link
Copy Markdown
Contributor

@jbureau92 jbureau92 left a comment

Choose a reason for hiding this comment

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

Thank you for this PR! The logic change to avoid flagging array copies inside conditional and try/catch blocks is correct and well-tested.

Few fixes are needed in changelog, see inline comments.

GCIRulesIT.java is still configured to expect 36 issues at the old line numbers, which means the tests will fail

Nice to have: Branch coverage is 79.2% on ArrayCopyCheck (just below the 80% threshold). Adding one more test covering an uncovered branch path would push it over the threshold.

Comment thread CHANGELOG.md

- Update ecocode-rules-specifications to 1.4.6

## [unreleased]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Doublon with L8

Comment thread CHANGELOG.md

- #193 GCI27 - reduce false positives for System.arraycopy suggestions inside conditional and try/catch blocks

## [2.1.2] - 2026-05-20
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Doublon with L23

Comment thread CHANGELOG.md

### Changed

- #193 GCI27 - reduce false positives for System.arraycopy suggestions inside conditional and try/catch blocks
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bad Format

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

2 participants