Skip to content

Commit

Permalink
Issue #11920: fix false positive UnusedLocalVariableCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuevKirill95 authored and romani committed Sep 26, 2022
1 parent 3eca7e0 commit 472fbc8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,11 @@ private static void visitIdentToken(DetailAST identAst, Deque<VariableDesc> vari
&& parent.getFirstChild() != identAst;
final boolean isConstructorReference = parent.getType() == TokenTypes.METHOD_REF
&& parent.getLastChild().getType() == TokenTypes.LITERAL_NEW;
final boolean isNestedClassInitialization =
TokenUtil.isOfType(identAst.getNextSibling(), TokenTypes.LITERAL_NEW)
&& parent.getType() == TokenTypes.DOT;

if (!isMethodReferenceMethodName
if (isNestedClassInitialization || !isMethodReferenceMethodName
&& !isConstructorReference
&& !TokenUtil.isOfType(parent, UNACCEPTABLE_PARENT_OF_IDENT)) {
checkIdentifierAst(identAst, variablesStack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,20 @@ public void testUnusedLocalVarNestedClasses2() throws Exception {
expected);
}

@Test
public void testUnusedLocalVarNestedClasses3() throws Exception {
final String[] expected = {
"36:17: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "p2"),
"54:13: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "o"),
"93:13: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "b"),
"95:13: " + getCheckMessage(MSG_UNUSED_LOCAL_VARIABLE, "a"),
};

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

@Test
public void testUnusedLocalVarEnum() throws Exception {
final String[] expected = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
UnusedLocalVariable
*/

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

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

public class InputUnusedLocalVariableNestedClasses3 {
void method() {
final OuterClass outerClass = new OuterClass(); // ok
final OuterClass.InnerClass innerClass = outerClass.new InnerClass();
System.out.println(innerClass);
}

class OuterClass {
class InnerClass {
InnerClass() { }
}
}


}

class Outer {
public class Inner { }
}

class Test {
void m() {
@A Outer p1 = new @A Outer(); // ok
// violation below 'Unused local variable 'p2'.'
@A Outer.@B Inner p2 = p1.new @B Inner();
}

@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
@interface A { }

@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
@interface B { }
}

class H006_ComplexConstructors<T> {
public <V> H006_ComplexConstructors(T t, V v) { }

class Inner3 {
Inner3(int x) {
H006_ComplexConstructors<Integer> instance = // ok
new <String>H006_ComplexConstructors<Integer>(0, "");
// violation below 'Unused local variable 'o'.'
Object o = instance.new Inner3(5).new <String>InnerInner3("hey");
}

class InnerInner3 {
<D> InnerInner3(D in) { }
}
}
}

class NullQualifiedNew2 {
class Inner {
Inner(int i) {}
}

public static void main(String[] args) {
int i = 1;
a: try {
NullQualifiedNew2 c = null;
c.new Inner(i++) {};
} catch (NullPointerException e) {
break a;
}
if (i != 1) throw new Error("i = " + i);
}
}

class T7090499<E> {

static class B<X> { }

class A<X> {
class X { }

class Z<Y> { }
}

T7090499 t = new T7090499() {
void test(Object arg1, B arg2) {
// violation below 'Unused local variable 'b'.'
boolean b = arg1 instanceof A;
// violation below 'Unused local variable 'a'.'
Object a = (A) arg1;
A a2 = new A() { };
a2.new Z() { };
}
};
}

0 comments on commit 472fbc8

Please sign in to comment.