Skip to content

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

Closed

Description

@Emilyaxe

Affects PMD Version: 7.24.0

Rule: UnnecessaryImport — https://docs.pmd-code.org/latest/pmd_rules_java_codestyle.html#unnecessaryimport

Description:

UnnecessaryImport incorrectly marks an import as unused when it is only referenced in Javadoc {@link} using an array type (e.g. Type[]), while correctly recognizing the same import as used when referenced as Type. This leads to inconsistent false positives.

Code Sample demonstrating the issue:

// BEFORE — no violation reported (correct)
package demo;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;

/**
 * See {@link Files#createDirectory(Path, FileAttribute)} for directory creation.
 */
public class Test {}
// AFTER — UnnecessaryImport reported on line 5 (false-positive)
package demo;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;

/**
 * See {@link Files#createDirectory(Path, FileAttribute[])} for directory creation.
 */
public class Test {}

Expected outcome: No UnnecessaryImport violation on the AFTER snippet — an import referenced from a Javadoc {@link} tag should be considered used regardless of whether the reference is written as T or T[].

Actual outcome: PMD reports Unused import 'java.nio.file.attribute.FileAttribute' at line 5 of AFTER, while correctly reporting nothing on BEFORE.

Running PMD through: CLI

Activity

  1. added
    a:false-positive
    on May 7, 2026
  2. UncleOwen commented on May 14, 2026

    @UncleOwen
    Member

    Related to #4325

  3. added a commit that references this issue on Aug 4, 2026
    8c0a935
  4. added this to the 7.27.0 milestone on Aug 4, 2026
  5. changed the title [-][java] UnnecessaryImport false-positive when Javadoc {@link} references an array type[/-] [+][java] UnnecessaryImport: False positive when Javadoc {@link} references an array type[/+] on Aug 20, 2026
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

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions