Skip to content

Commit

Permalink
Issue checkstyle#14092: fix UnusedLocalVariableCheck to support local…
Browse files Browse the repository at this point in the history
… classes
  • Loading branch information
Lmh-java committed Apr 6, 2024
1 parent f7e952b commit eff887d
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ else if (isNonLocalTypeDeclaration(ast)) {
private static void visitDotToken(DetailAST dotAst, Deque<VariableDesc> variablesStack) {
if (dotAst.getParent().getType() != TokenTypes.LITERAL_NEW
&& shouldCheckIdentTokenNestedUnderDot(dotAst)) {
checkIdentifierAst(dotAst.findFirstToken(TokenTypes.IDENT), variablesStack);
final DetailAST identifier = dotAst.findFirstToken(TokenTypes.IDENT);
if (identifier != null) {
checkIdentifierAst(dotAst.findFirstToken(TokenTypes.IDENT), variablesStack);
}
}
}

Expand Down Expand Up @@ -420,14 +423,15 @@ private static DetailAST getBlockContainingLocalAnonInnerClass(DetailAST literal
private static void addLocalVariables(DetailAST varDefAst, Deque<VariableDesc> variablesStack) {
final DetailAST parentAst = varDefAst.getParent();
final DetailAST grandParent = parentAst.getParent();
final boolean isInstanceVarInAnonymousInnerClass =
grandParent.getType() == TokenTypes.LITERAL_NEW;
if (isInstanceVarInAnonymousInnerClass
final boolean isInstanceVarInInnerClass =
grandParent.getType() == TokenTypes.LITERAL_NEW
|| grandParent.getType() == TokenTypes.CLASS_DEF;
if (isInstanceVarInInnerClass
|| parentAst.getType() != TokenTypes.OBJBLOCK) {
final DetailAST ident = varDefAst.findFirstToken(TokenTypes.IDENT);
final VariableDesc desc = new VariableDesc(ident.getText(),
varDefAst.findFirstToken(TokenTypes.TYPE), findScopeOfVariable(varDefAst));
if (isInstanceVarInAnonymousInnerClass) {
if (isInstanceVarInInnerClass) {
desc.registerAsInstOrClassVar();
}
variablesStack.push(desc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ public void testUnusedLocalVar2() throws Exception {
expected);
}

@Test
public void testUnusedLocalVar3() throws Exception {
final String[] expected = {
"21:13: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "a"),
};
verifyWithInlineConfigParser(
getPath("InputUnusedLocalVariable3.java"),
expected);
}

@Test
public void testUnusedLocalVarInAnonInnerClasses() throws Exception {
final String[] expected = {
Expand Down Expand Up @@ -241,6 +251,47 @@ public void testUnusedLocalVarNestedClasses3() throws Exception {
expected);
}

@Test
public void testUnusedLocalVarNestedClasses4() throws Exception {
final String[] expected = {
"12:5: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "a"),
"13:5: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "ab"),
};

verifyWithInlineConfigParser(
getPath("InputUnusedLocalVariableNestedClasses4.java"),
expected);
}

@Test
public void testUnusedLocalVarNestedClasses5() throws Exception {
final String[] expected = {
"12:5: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "a"),
"13:5: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "ab"),
"19:11: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "abc"),
};

verifyWithInlineConfigParser(
getPath("InputUnusedLocalVariableNestedClasses5.java"),
expected);
}

@Test
public void testUnusedLocalVarNestedClasses6() throws Exception {
final String[] expected = {
"10:5: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "a"),
"11:5: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "ab"),
"16:9: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "ab"),
"23:9: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "a"),
"24:9: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "ab"),
"28:13: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "a"),
};

verifyWithInlineConfigParser(
getPath("InputUnusedLocalVariableNestedClasses6.java"),
expected);
}

@Test
public void testUnusedLocalVarTestWarningSeverity() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
UnusedLocalVariable
*/

package com.puppycrawl.tools.checkstyle.checks.coding.unusedlocalvariable;

public class InputUnusedLocalVariable3 {
class Parent {
protected int a = 0;
public Parent(Child d) {
a = 1;
}
}

class Child extends InputUnusedLocalVariable3.Parent {
protected int b = 0;
public Child(Child d) {
InputUnusedLocalVariable3.this.super(d);
int a = 0; // violation, unused variable 'a'
System.out.println(super.a);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
UnusedLocalVariable
*/
package com.puppycrawl.tools.checkstyle.checks.coding.unusedlocalvariable;

public class InputUnusedLocalVariableNestedClasses4 {
int a = 12;

void foo() {
int a = 12; // violation, unused variable 'a'
int ab = 12; // violation, unused variable 'ab'

class asd {
Test a = new Test() {
void asd() {
System.out.println(a);
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
UnusedLocalVariable
*/
package com.puppycrawl.tools.checkstyle.checks.coding.unusedlocalvariable;

public class InputUnusedLocalVariableNestedClasses5 {
int a = 12;

void foo() {
int a = 12; // violation, unused variable 'a'
int ab = 12; // violation, unused variable 'ab'

class abc {
Test a = new Test() {
void abc() {
System.out.println(a);
int abc = 10; // violation, unused variable 'abc'

class def {
Test abc = new Test() {
void def() {
System.out.println(abc);
}
};
}
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
UnusedLocalVariable
*/
package com.puppycrawl.tools.checkstyle.checks.coding.unusedlocalvariable;

public class InputUnusedLocalVariableNestedClasses6 {
void foo() {
int a = 12; // violation, unused variable 'a'
int ab = 12; // violation, unused variable 'ab'

class Ghi {
void foo() {
int a = 12;
int ab = 12; // violation, unused variable 'ab'
System.out.println(a);
}
}

class Def {
void foo() {
int a = 12; // violation, unused variable 'a'
int ab = 12; // violation, unused variable 'ab'

class InnerDef {
void foo() {
int a = 12; // violation, unused variable 'a'
int ab = 12;
System.out.println(ab);
}
}
}
}
}
}

0 comments on commit eff887d

Please sign in to comment.