Repository: checkstyle/checkstyle

Problem statement:
UnusedImports error if there are only array references to a given class
```bash
slovdahl@desk:~/checkstyle-unused-import-bug-reproducer/src/main/java (main)$ javac checkstyle/reproducer/UnusedImportBug.java 
slovdahl@desk:~/checkstyle-unused-import-bug-reproducer/src/main/java (main)$ 

slovdahl@desk:~/checkstyle-unused-import-bug-reproducer (main)$ cat gradle/checkstyle/checkstyle.xml 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="UnusedImports">
            <property name="processJavadoc" value="true"/>
        </module>
    </module>
</module>


slovdahl@desk:~/checkstyle-unused-import-bug-reproducer (main)$ cat src/main/java/checkstyle/reproducer/UnusedImportBug.java 
package checkstyle.reproducer;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class UnusedImportBug {

    private static final Set<String> FOO;
    static {
        FOO = new HashSet<>();

        FOO.add( HashMap[].class.getName() );
    }
}


slovdahl@desk:~/checkstyle-unused-import-bug-reproducer (main)$ RUN_LOCALE="-Duser.language=en -Duser.country=US"
slovdahl@desk:~/checkstyle-unused-import-bug-reproducer (main)$ java $RUN_LOCALE -jar checkstyle-9.0.1-all.jar -c gradle/checkstyle/checkstyle.xml src/main/java/checkstyle/reproducer/UnusedImportBug.java
Starting audit...
[ERROR] /home/slovdahl/checkstyle-unused-import-bug-reproducer/src/main/java/checkstyle/reproducer/UnusedImportBug.java:3:8: Unused import - java.util.HashMap. [UnusedImports]
Audit done.
Checkstyle ends with 1 errors.

```

---

**Describe what you expect in detail.**

The `UnusedImport` error should not be triggered if an import is actually used.

---

MCVE: https://github.com/hiboxsystems/checkstyle-unused-import-bug-reproducer 


Hints:
AST:
```
COMPILATION_UNIT -> COMPILATION_UNIT [1:0]
|--IMPORT -> import [1:0]
|   |--DOT -> . [1:16]
|   |   |--DOT -> . [1:11]
|   |   |   |--IDENT -> java [1:7]
|   |   |   `--IDENT -> util [1:12]
|   |   `--IDENT -> HashMap [1:17]
|   `--SEMI -> ; [1:24]
|--IMPORT -> import [2:0]
|   |--DOT -> . [2:16]
|   |   |--DOT -> . [2:11]
|   |   |   |--IDENT -> java [2:7]
|   |   |   `--IDENT -> util [2:12]
|   |   `--IDENT -> HashSet [2:17]
|   `--SEMI -> ; [2:24]
|--IMPORT -> import [3:0]
|   |--DOT -> . [3:16]
|   |   |--DOT -> . [3:11]
|   |   |   |--IDENT -> java [3:7]
|   |   |   `--IDENT -> util [3:12]
|   |   `--IDENT -> Set [3:17]
|   `--SEMI -> ; [3:20]
`--CLASS_DEF -> CLASS_DEF [5:0]
    |--MODIFIERS -> MODIFIERS [5:0]
    |   `--LITERAL_PUBLIC -> public [5:0]
    |--LITERAL_CLASS -> class [5:7]
    |--IDENT -> UnusedImportBug [5:13]
    `--OBJBLOCK -> OBJBLOCK [5:29]
        |--LCURLY -> { [5:29]
        |--VARIABLE_DEF -> VARIABLE_DEF [7:4]
        |   |--MODIFIERS -> MODIFIERS [7:4]
        |   |   |--LITERAL_PRIVATE -> private [7:4]
        |   |   |--LITERAL_STATIC -> static [7:12]
        |   |   `--FINAL -> final [7:19]
        |   |--TYPE -> TYPE [7:25]
        |   |   |--IDENT -> Set [7:25]
        |   |   `--TYPE_ARGUMENTS -> TYPE_ARGUMENTS [7:28]
        |   |       |--GENERIC_START -> < [7:28]
        |   |       |--TYPE_ARGUMENT -> TYPE_ARGUMENT [7:29]
        |   |       |   `--IDENT -> String [7:29]
        |   |       `--GENERIC_END -> > [7:35]
        |   |--IDENT -> FOO [7:37]
        |   `--SEMI -> ; [7:40]
        |--STATIC_INIT -> STATIC_INIT [8:4]
        |   `--SLIST -> { [8:11]
        |       |--EXPR -> EXPR [9:12]
        |       |   `--ASSIGN -> = [9:12]
        |       |       |--IDENT -> FOO [9:8]
        |       |       `--LITERAL_NEW -> new [9:14]
        |       |           |--IDENT -> HashSet [9:18]
        |       |           |--TYPE_ARGUMENTS -> TYPE_ARGUMENTS [9:25]
        |       |           |   |--GENERIC_START -> < [9:25]
        |       |           |   `--GENERIC_END -> > [9:26]
        |       |           |--LPAREN -> ( [9:27]
        |       |           |--ELIST -> ELIST [9:28]
        |       |           `--RPAREN -> ) [9:28]
        |       |--SEMI -> ; [9:29]
        |       |--EXPR -> EXPR [11:15]
        |       |   `--METHOD_CALL -> ( [11:15]
        |       |       |--DOT -> . [11:11]
        |       |       |   |--IDENT -> FOO [11:8]
        |       |       |   `--IDENT -> add [11:12]
        |       |       |--ELIST -> ELIST [11:40]
        |       |       |   `--EXPR -> EXPR [11:40]
        |       |       |       `--METHOD_CALL -> ( [11:40]
        |       |       |           |--DOT -> . [11:32]
        |       |       |           |   |--DOT -> . [11:26]
        |       |       |           |   |   |--IDENT -> HashMap [11:17]
        |       |       |           |   |   |--ARRAY_DECLARATOR -> [ [11:24]
        |       |       |           |   |   |   `--RBRACK -> ] [11:25]
        |       |       |           |   |   `--LITERAL_CLASS -> class [11:27]
        |       |       |           |   `--IDENT -> getName [11:33]
        |       |       |           |--ELIST -> ELIST [11:41]
        |       |       |           `--RPAREN -> ) [11:41]
        |       |       `--RPAREN -> ) [11:43]
        |       |--SEMI -> ; [11:44]
        |       `--RCURLY -> } [12:4]
        `--RCURLY -> } [13:0]
```
This may be related to #10100
I am on it

Patch:
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck.java
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck.java
@@ -272,17 +272,13 @@ private void processIdent(DetailAST ast) {
                 || parentType == TokenTypes.METHOD_DEF;
 
         final boolean isQualifiedIdent = parentType == TokenTypes.DOT
+                && !TokenUtil.isOfType(ast.getPreviousSibling(), TokenTypes.DOT)
                 && ast.getNextSibling() != null;
 
-        final boolean isQualifiedNameArrayType = parent.getParent().getType() == TokenTypes.DOT
-                && ast.getNextSibling() != null
-                && ast.getNextSibling().getType() == TokenTypes.ARRAY_DECLARATOR;
-
         if (TokenUtil.isTypeDeclaration(parentType)) {
             currentFrame.addDeclaredType(ast.getText());
         }
-        else if ((!isPossibleDotClassOrInMethod || isQualifiedIdent)
-                    && !isQualifiedNameArrayType) {
+        else if (!isPossibleDotClassOrInMethod || isQualifiedIdent) {
             currentFrame.addReferencedType(ast.getText());
         }
     }


Test patch:
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheckTest.java
--- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheckTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheckTest.java
@@ -145,6 +145,15 @@ public void testAnnotations() throws Exception {
                 getNonCompilablePath("InputUnusedImportsAnnotations.java"), expected);
     }
 
+    @Test
+    public void testArrayRef() throws Exception {
+        final String[] expected = {
+            "13:8: " + getCheckMessage(MSG_KEY, "java.util.ArrayList"),
+        };
+        verifyWithInlineConfigParser(
+                getPath("InputUnusedImportsArrayRef.java"), expected);
+    }
+
     @Test
     public void testBug() throws Exception {
         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/imports/unusedimports/InputUnusedImportsArrayRef.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/imports/unusedimports/InputUnusedImportsArrayRef.java
new file mode 100644
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/imports/unusedimports/InputUnusedImportsArrayRef.java
@@ -0,0 +1,24 @@
+/*
+UnusedImports
+processJavadoc = (default)true
+
+
+*/
+
+package com.puppycrawl.tools.checkstyle.checks.imports.unusedimports;
+
+import java.util.HashMap; // ok
+import java.util.HashSet;
+import java.util.Set;
+import java.util.ArrayList; // violation
+
+public class InputUnusedImportsArrayRef {
+
+    private static final Set<String> FOO;
+    static {
+        FOO = new HashSet<>();
+
+        FOO.add( HashMap[].class.getName() );
+        FOO.add( java.util.ArrayList[].class.getName() );
+    }
+}