disable convert to functional in var context (IDEA-185038)

This commit is contained in:
Anna.Kozlova
2018-01-17 13:03:03 +01:00
parent 77d83d1255
commit de82603306
6 changed files with 27 additions and 6 deletions

View File

@@ -78,10 +78,7 @@ public class AnonymousCanBeLambdaInspection extends AbstractBaseJavaLocalInspect
public void visitAnonymousClass(final PsiAnonymousClass aClass) {
super.visitAnonymousClass(aClass);
final PsiElement parent = aClass.getParent();
final PsiElement lambdaContext = parent != null ? parent.getParent() : null;
if (lambdaContext != null &&
(LambdaUtil.isValidLambdaContext(lambdaContext) || !(lambdaContext instanceof PsiExpressionStatement)) &&
canBeConvertedToLambda(aClass, false, isOnTheFly || reportNotAnnotatedInterfaces, Collections.emptySet())) {
if (canBeConvertedToLambda(aClass, false, isOnTheFly || reportNotAnnotatedInterfaces, Collections.emptySet())) {
final PsiElement lBrace = aClass.getLBrace();
LOG.assertTrue(lBrace != null);
final TextRange rangeInElement = new TextRange(0, aClass.getStartOffsetInParent() + lBrace.getStartOffsetInParent());
@@ -199,6 +196,9 @@ public class AnonymousCanBeLambdaInspection extends AbstractBaseJavaLocalInspect
boolean acceptParameterizedFunctionTypes,
boolean reportNotAnnotatedInterfaces,
@NotNull Set<String> ignoredRuntimeAnnotations) {
PsiElement parent = aClass.getParent();
final PsiElement lambdaContext = parent != null ? parent.getParent() : null;
if (lambdaContext == null || !LambdaUtil.isValidLambdaContext(lambdaContext) && !(lambdaContext instanceof PsiReferenceExpression)) return false;
if (PsiUtil.getLanguageLevel(aClass).isAtLeast(LanguageLevel.JDK_1_8)) {
final PsiClassType baseClassType = aClass.getBaseClassType();
final PsiClassType.ClassResolveResult resolveResult = baseClassType.resolveGenerics();

View File

@@ -52,6 +52,7 @@ public class UnnecessaryModuleDependencyInspection extends GlobalInspectionTool
}
final RefManager refManager = globalContext.getRefManager();
currentDependencies:
for (final OrderEntry entry : declaredDependencies) {
if (entry instanceof ModuleOrderEntry && ((ModuleOrderEntry)entry).getScope() != DependencyScope.RUNTIME) {
final Module dependency = ((ModuleOrderEntry)entry).getModule();
@@ -62,6 +63,7 @@ public class UnnecessaryModuleDependencyInspection extends GlobalInspectionTool
final Iterator<Module> iterator = graph.getOut(module);
while (iterator.hasNext()) {
final Module dep = iterator.next();
if (!scope.containsModule(dep)) continue currentDependencies;
final RefModule depRefModule = refManager.getRefModule(dep);
if (depRefModule != null) {
final Set<Module> neededModules = depRefModule.getUserData(UnnecessaryModuleDependencyAnnotator.DEPENDENCIES);

View File

@@ -143,10 +143,15 @@ public class LambdaUtil {
return context instanceof PsiLambdaExpression ||
context instanceof PsiReturnStatement ||
context instanceof PsiAssignmentExpression ||
context instanceof PsiVariable ||
context instanceof PsiVariable && !withInferredType((PsiVariable)context) ||
context instanceof PsiArrayInitializerExpression;
}
private static boolean withInferredType(PsiVariable variable) {
PsiTypeElement typeElement = variable.getTypeElement();
return typeElement != null && typeElement.isInferredType();
}
@Contract("null -> null")
@Nullable
public static MethodSignature getFunction(final PsiClass psiClass) {

View File

@@ -0,0 +1,9 @@
class Test {
{
var r = new Runnable() {
public void run() {
System.out.println();
}
};
}
}

View File

@@ -29,7 +29,7 @@ class Main {
<error descr="Cannot infer type: lambda expression requires an explicit target type">var</error> f = () -> "hello";
<error descr="Cannot infer type: method reference requires an explicit target type">var</error> m = Main::localVariableDeclaration;
<error descr="Cannot infer type: variable initializer is 'null'">var</error> g = null;
var runnable = true ? <error descr="<lambda expression> is not a functional interface">() -> {}</error> : <error descr="<lambda expression> is not a functional interface">() -> {}</error>;
var runnable = true ? <error descr="Lambda expression not expected here">() -> {}</error> : <error descr="Lambda expression not expected here">() -> {}</error>;
}
private void forEachType(String[] strs, Iterable<String> it, Iterable raw) {

View File

@@ -16,6 +16,7 @@
package com.intellij.java.codeInsight.daemon;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import com.intellij.codeInspection.AnonymousCanBeLambdaInspection;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.pom.java.LanguageLevel;
@@ -36,6 +37,10 @@ public class LightAdvLVTIHighlightingTest extends LightDaemonAnalyzerTestCase {
}
public void testSimpleAvailability() { doTest(); }
public void testDisabledInspections() {
enableInspectionTool(new AnonymousCanBeLambdaInspection());
doTest(BASE_PATH + "/" + getTestName(false) + ".java", true, false);
}
public void testVarClassNameConflicts() { doTest(); }
public void testStandaloneInVarContext() { doTest(); }
public void testUpwardProjection() { doTest(); }