Skip to content

Commit 073289a

Browse files
strkkkrnveach
authored andcommitted
Issue #10855: fix unused imports check for array references
1 parent 825af33 commit 073289a

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheck.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,17 +272,13 @@ private void processIdent(DetailAST ast) {
272272
|| parentType == TokenTypes.METHOD_DEF;
273273

274274
final boolean isQualifiedIdent = parentType == TokenTypes.DOT
275+
&& !TokenUtil.isOfType(ast.getPreviousSibling(), TokenTypes.DOT)
275276
&& ast.getNextSibling() != null;
276277

277-
final boolean isQualifiedNameArrayType = parent.getParent().getType() == TokenTypes.DOT
278-
&& ast.getNextSibling() != null
279-
&& ast.getNextSibling().getType() == TokenTypes.ARRAY_DECLARATOR;
280-
281278
if (TokenUtil.isTypeDeclaration(parentType)) {
282279
currentFrame.addDeclaredType(ast.getText());
283280
}
284-
else if ((!isPossibleDotClassOrInMethod || isQualifiedIdent)
285-
&& !isQualifiedNameArrayType) {
281+
else if (!isPossibleDotClassOrInMethod || isQualifiedIdent) {
286282
currentFrame.addReferencedType(ast.getText());
287283
}
288284
}

src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/UnusedImportsCheckTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ public void testAnnotations() throws Exception {
145145
getNonCompilablePath("InputUnusedImportsAnnotations.java"), expected);
146146
}
147147

148+
@Test
149+
public void testArrayRef() throws Exception {
150+
final String[] expected = {
151+
"13:8: " + getCheckMessage(MSG_KEY, "java.util.ArrayList"),
152+
};
153+
verifyWithInlineConfigParser(
154+
getPath("InputUnusedImportsArrayRef.java"), expected);
155+
}
156+
148157
@Test
149158
public void testBug() throws Exception {
150159
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
UnusedImports
3+
processJavadoc = (default)true
4+
5+
6+
*/
7+
8+
package com.puppycrawl.tools.checkstyle.checks.imports.unusedimports;
9+
10+
import java.util.HashMap; // ok
11+
import java.util.HashSet;
12+
import java.util.Set;
13+
import java.util.ArrayList; // violation
14+
15+
public class InputUnusedImportsArrayRef {
16+
17+
private static final Set<String> FOO;
18+
static {
19+
FOO = new HashSet<>();
20+
21+
FOO.add( HashMap[].class.getName() );
22+
FOO.add( java.util.ArrayList[].class.getName() );
23+
}
24+
}

0 commit comments

Comments
 (0)