unused return value inspection: method reference awareness (IDEA-134991)

This commit is contained in:
Anna Kozlova
2015-01-07 12:21:52 +01:00
parent 49a192f27f
commit 49f82c1948
4 changed files with 38 additions and 1 deletions
@@ -211,6 +211,14 @@ public class RefJavaUtilImpl extends RefJavaUtil{
PsiMethod psiMethod = (PsiMethod)psiResolved;
RefMethodImpl refMethod = (RefMethodImpl)refResolved;
if (refExpression instanceof PsiMethodReferenceExpression) {
PsiType returnType = psiMethod.getReturnType();
if (!psiMethod.isConstructor() && returnType != PsiType.VOID) {
refMethod.setReturnValueUsed(true);
addTypeReference(psiFrom, returnType, refFrom.getRefManager());
}
return;
}
PsiMethodCallExpression call = PsiTreeUtil.getParentOfType(
refExpression,
PsiMethodCallExpression.class
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1,13 @@
import java.util.function.Consumer;
import java.util.function.DoubleSupplier;
class B {
public void test(Consumer<DoubleSupplier> consumer) {
consumer.accept(this::method);
}
private double method() {
return 1;
}
}
@@ -12,6 +12,10 @@ package com.intellij.codeInspection;
import com.intellij.JavaTestUtil;
import com.intellij.codeInspection.unusedReturnValue.UnusedReturnValue;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.InspectionTestCase;
public class UnusedReturnValueTest extends InspectionTestCase {
@@ -39,7 +43,17 @@ public class UnusedReturnValueTest extends InspectionTestCase {
doTest();
}
public void testMethodReference() throws Exception {
doTest();
}
@Override
protected Sdk getTestProjectSdk() {
Sdk sdk = IdeaTestUtil.getMockJdk18();
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_8);
return sdk;
}
public void testSimpleSetter() throws Exception {
try {
myTool.IGNORE_BUILDER_PATTERN = true;