Skip to content

Commit

Permalink
Issue #10855: fix unused imports check for array references
Browse files Browse the repository at this point in the history
  • Loading branch information
strkkk authored and rnveach committed Oct 25, 2021
1 parent 825af33 commit 073289a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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() );
}
}

0 comments on commit 073289a

Please sign in to comment.