From 66cd10b00c966556dad6418f0eb9a133b62fca1c Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 30 Oct 2014 20:38:39 +0100 Subject: [PATCH] new inference: unchecked conversion detection in case of inference variables (IDEA-122932) --- .../constraints/TypeCompatibilityConstraint.java | 8 +++++++- .../UncheckedConversionWithRecursiveTypeParams.java | 13 +++++++++++++ .../lambda/GraphInferenceHighlightingTest.java | 4 ++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/graphInference/UncheckedConversionWithRecursiveTypeParams.java diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/TypeCompatibilityConstraint.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/TypeCompatibilityConstraint.java index f01c82cae769..7b76a68ab5d3 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/TypeCompatibilityConstraint.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/TypeCompatibilityConstraint.java @@ -17,6 +17,7 @@ package com.intellij.psi.impl.source.resolve.graphInference.constraints; import com.intellij.psi.*; import com.intellij.psi.impl.source.resolve.graphInference.InferenceSession; +import com.intellij.psi.impl.source.resolve.graphInference.InferenceVariable; import com.intellij.psi.util.PsiUtil; import com.intellij.psi.util.TypeConversionUtil; import org.jetbrains.annotations.NotNull; @@ -72,7 +73,12 @@ public class TypeCompatibilityConstraint implements ConstraintFormula { final PsiClass sClass = sResult.getElement(); if (tClass != null && sClass != null) { final PsiSubstitutor sSubstitutor = TypeConversionUtil.getClassSubstitutor(tClass, sClass, sResult.getSubstitutor()); - if (sSubstitutor != null && PsiUtil.isRawSubstitutor(tClass, sSubstitutor)) { + if (sSubstitutor != null) { + if (PsiUtil.isRawSubstitutor(tClass, sSubstitutor)) { + return true; + } + } + else if (tClass instanceof InferenceVariable && ((PsiClassType)s).isRaw() && tClass.isInheritor(sClass, true)) { return true; } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/graphInference/UncheckedConversionWithRecursiveTypeParams.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/graphInference/UncheckedConversionWithRecursiveTypeParams.java new file mode 100644 index 000000000000..f9fc707a2924 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/graphInference/UncheckedConversionWithRecursiveTypeParams.java @@ -0,0 +1,13 @@ + +import java.util.List; + +class Foo { + + public void test(Foo parent) { + Foo foo = getElements(parent).get(0); + } + + public static > List getElements(E parent) { + return null; + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GraphInferenceHighlightingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GraphInferenceHighlightingTest.java index 122e776fda9d..acec098d6a5b 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GraphInferenceHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GraphInferenceHighlightingTest.java @@ -255,6 +255,10 @@ public class GraphInferenceHighlightingTest extends LightDaemonAnalyzerTestCase doTest(); } + public void testUncheckedConversionWithRecursiveTypeParams() throws Exception { + doTest(false); + } + private void doTest() throws Exception { doTest(false); }