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 Mar 18, 2024
1 parent 386360b commit 4503374
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ private static void addLocalVariables(DetailAST varDefAst, Deque<VariableDesc> v
final DetailAST parentAst = varDefAst.getParent();
final DetailAST grandParent = parentAst.getParent();
final boolean isInstanceVarInAnonymousInnerClass =
grandParent.getType() == TokenTypes.LITERAL_NEW;
grandParent.getType() == TokenTypes.LITERAL_NEW
|| grandParent.getType() == TokenTypes.CLASS_DEF;
if (isInstanceVarInAnonymousInnerClass
|| parentAst.getType() != TokenTypes.OBJBLOCK) {
final DetailAST ident = varDefAst.findFirstToken(TokenTypes.IDENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,47 @@ public void testUnusedLocalVarNestedClasses3() throws Exception {
expected);
}

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

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

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

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

@Test
public void testUnusedLocalVarNestedClasses6() throws Exception {
final String[] expected = {
"16:5: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "a"),
"17:5: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "ab"),
"22:9: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "ab"),
"29:9: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "a"),
"30:9: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "ab"),
"34: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,29 @@
/*xml
<module name="Checker">
<property name="charset" value="UTF-8" />
<property name="severity" value="warning" />
<property name="haltOnException" value="false" />
<module name="TreeWalker">
<module name="UnusedLocalVariable">
</module>
</module>
</module>
*/
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,38 @@
/*xml
<module name="Checker">
<property name="charset" value="UTF-8" />
<property name="severity" value="warning" />
<property name="haltOnException" value="false" />
<module name="TreeWalker">
<module name="UnusedLocalVariable">
</module>
</module>
</module>
*/
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,42 @@
/*xml
<module name="Checker">
<property name="charset" value="UTF-8" />
<property name="severity" value="warning" />
<property name="haltOnException" value="false" />
<module name="TreeWalker">
<module name="UnusedLocalVariable">
</module>
</module>
</module>
*/
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 4503374

Please sign in to comment.