unused parameter: do not highlight if method is used locally by method reference (IDEA-123265)

This commit is contained in:
Anna Kozlova
2014-04-08 17:54:55 +02:00
parent e14e21af5e
commit 6fd18d16b3
4 changed files with 29 additions and 3 deletions

View File

@@ -507,6 +507,12 @@ public class PostHighlightingPass extends ProgressableTextEditorHighlightingPass
@NotNull PsiIdentifier identifier,
@NotNull ProgressIndicator progress) {
if (!myRefCountHolder.isReferenced(parameter) && !isImplicitUsage(myProject, parameter, progress)) {
//parameter is defined by functional interface
final PsiElement declarationScope = parameter.getDeclarationScope();
if (declarationScope instanceof PsiMethod &&
myRefCountHolder.isReferencedByMethodReference((PsiMethod)declarationScope)) {
return null;
}
String message = JavaErrorMessages.message("parameter.is.not.used", identifier.getText());
return createUnusedSymbolInfo(identifier, message, HighlightInfoType.UNUSED_SYMBOL);
}

View File

@@ -203,6 +203,26 @@ public class RefCountHolder {
return usedStatus == Boolean.TRUE;
}
public boolean isReferencedByMethodReference(@NotNull PsiMethod method) {
if (!PsiUtil.isLanguageLevel8OrHigher(method)) return false;
List<PsiReference> array;
synchronized (myLocalRefsMap) {
array = myLocalRefsMap.getKeysByValue(method);
}
if (array != null && !array.isEmpty()) {
for (PsiReference reference : array) {
final PsiElement element = reference.getElement();
if (element != null && element instanceof PsiMethodReferenceExpression) {
return true;
}
}
}
return false;
}
private static boolean isParameterUsedRecursively(@NotNull PsiElement element, @NotNull List<PsiReference> array) {
if (!(element instanceof PsiParameter)) return false;
PsiParameter parameter = (PsiParameter)element;

View File

@@ -77,7 +77,7 @@ abstract class AbstractCollection<E> implements Collection<E> {
public boolean add(E e) {
return true;
}
public boolean addAll(Collection<? extends E> <warning descr="Parameter 'c' is never used">c</warning>) {
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
return modified;
}

View File

@@ -6,11 +6,11 @@ class Test {
Test m(List<Integer> l1, List<Integer> l2);
}
static Test meth(List<Integer>... <warning descr="Parameter 'lli' is never used">lli</warning>) {
static Test meth(List<Integer>... lli) {
return null;
}
Test(List<Integer>... <warning descr="Parameter 'lli' is never used">lli</warning>) {}
Test(List<Integer>... lli) {}
{
I <warning descr="Variable 'i1' is never used">i1</warning> = <warning descr="Unchecked generics array creation for varargs parameter">Test::meth</warning>;