Repository navigation
[java] Fix #6651: UnnecessaryImport: False positive for array-typed Javadoc {@link} parameters - #6884
Conversation
…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
|
Compared to main: (comment created at 2026-08-04 14:09:57+00:00 for a93b0bc) |
…ort-javadoc-array-params # Conflicts: # docs/pages/release_notes.md
|
Since the Regression Tester can't produce a fresh report right now (the Violation diff is That report still describes this PR's behaviour: |
Fixes #6651.
UnnecessaryImportRuleparses 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 importFileAttribute, 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 inUnnecessaryImport.xmlreproducing the exact snippet from the issue.