Skip to content

[java] Fix #6651: UnnecessaryImport: False positive for array-typed Javadoc {@link} parameters - #6884

Merged
UncleOwen merged 4 commits into
pmd:mainfrom
hexonal:fix-unnecessary-import-javadoc-array-params
Aug 4, 2026
Merged

UncleOwen merged 4 commits into
pmd:mainfrom
hexonal:fix-unnecessary-import-javadoc-array-params

Conversation

@hexonal

@hexonal hexonal commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #6651.

UnnecessaryImportRule parses Javadoc {@link}/{@see}/{@value} references with a regex to find type references and mark the corresponding imports as used. When a referenced method parameter has an array type, e.g. {@link Files#createDirectory(Path, FileAttribute[])}, the captured parameter string keeps the trailing []. That string is then compared directly against each import's simple name, so "FileAttribute[]" never matches the import FileAttribute, and the import is wrongly reported as unused.

This adds a small helper, stripArrayOrVarargsSuffix, that strips a trailing ... (varargs) and any trailing [] pairs off each parsed parameter type before it's matched against single-name and on-demand imports. Added a regression test case in UnnecessaryImport.xml reproducing the exact snippet from the issue.

…arams

When a Javadoc {@link}/{@see} reference includes a method parameter with
an array type, e.g. {@link Foo#bar(Type[])}, the regex-captured parameter
string kept the trailing "[]" and was compared as-is against the imported
simple name. "Type[]" never equals "Type", so the import was wrongly
flagged as unused.

Strip a trailing "..." (varargs) and any trailing "[]" pairs off each
captured parameter type before matching it against single-name and
on-demand imports.

Fixes pmd#6651
@pmd-actions-helper

pmd-actions-helper Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 2 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-08-04 14:09:57+00:00 for a93b0bc)

@hexonal

hexonal commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

Since the Regression Tester can't produce a fresh report right now (the Schedul-o-matic-9000 outage), here's the archived one from before it broke:

https://pull-requests.pmd-code.org/pr-6884/eb41f137d12b559047eb6b79ac775ae47a570596/regression/index.html

Violation diff is +0 across all 9 projects, with -1 on openjdk-11 and -1 on spring-framework — i.e. two false positives removed and nothing new reported. Base main 5c69c139 vs HEAD 78110a6d.

That report still describes this PR's behaviour: UnnecessaryImportRule.java hasn't changed since eb41f137 (only upstream-main merges followed), and every Build Pull Request job is green.

@UncleOwen UncleOwen left a comment

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.

Thanks!

@UncleOwen
UncleOwen merged commit 8c0a935 into pmd:main Aug 4, 2026
13 checks passed
@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 4, 2026
@adangel adangel changed the title [java] Fix UnnecessaryImport false positive for array-typed Javadoc {@link} parameters [java] Fix #6651: UnnecessaryImport: False positive for array-typed Javadoc {@link} parameters Aug 20, 2026
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.

[java] UnnecessaryImport: False positive when Javadoc {@link} references an array type

2 participants