Skip to content

filterDependencies destructively mutates the caller's set via Iterator.remove() #1646

Description

@elharo

AbstractAnalyzeMojo.filterDependencies() at line 568-573:

for (Iterator<Artifact> it = artifacts.iterator(); it.hasNext(); ) {
    Artifact artifact = it.next();
    if (!filter.include(artifact)) {
        it.remove();    // mutates the ORIGINAL set passed in
        result.add(artifact);
    }
}

This method is called multiple times on the same set with different filter arrays (lines 363-375):

ignoredUsedUndeclared.addAll(filterDependencies(usedUndeclaredWithClasses.keySet(), ignoredDependencies));
ignoredUsedUndeclared.addAll(filterDependencies(usedUndeclaredWithClasses.keySet(), ignoredUsedUndeclaredDependencies));

After the first non-empty filter removes matching entries from usedUndeclaredWithClasses, the second call operates on an already-reduced set. The "Used undeclared dependencies found:" warning listing (line 392, 394) iterates usedUndeclaredWithClasses which has been destructively modified — entries matching ignoredDependencies are silently missing from the warning output.

The same pattern applies to unusedDeclared and nonTestScope (lines 367-375).

The method should either:

  1. Copy the set before iterating, or
  2. Build the result without removing from the input, leaving the caller responsible for subtraction

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions