ExpectedTypesProvider: do not suggest null[] type if iteration parameter type is null

Fixes IDEA-207987 Create local variable suggested but fails with exception
This commit is contained in:
Tagir Valeev
2019-02-27 15:12:27 +07:00
parent 313492874b
commit d38b98dfe4
3 changed files with 25 additions and 1 deletions

View File

@@ -487,6 +487,8 @@ public class ExpectedTypesProvider {
if (myExpr.equals(statement.getIteratedValue())) {
PsiType type = statement.getIterationParameter().getType();
if (PsiType.NULL.equals(type)) return;
PsiType arrayType = type.createArrayType();
myResult.add(createInfoImpl(arrayType, arrayType));
@@ -494,7 +496,7 @@ public class ExpectedTypesProvider {
PsiElementFactory factory = JavaPsiFacade.getElementFactory(manager.getProject());
PsiClass iterableClass =
JavaPsiFacade.getInstance(manager.getProject()).findClass("java.lang.Iterable", statement.getResolveScope());
if (iterableClass != null && iterableClass.getTypeParameters().length == 1 && !PsiType.NULL.equals(type)) {
if (iterableClass != null && iterableClass.getTypeParameters().length == 1) {
Map<PsiTypeParameter, PsiType> map = new HashMap<>();
map.put(iterableClass.getTypeParameters()[0], PsiWildcardType.createExtends(manager, type));
PsiType iterableType = factory.createType(iterableClass, factory.createSubstitutor(map));

View File

@@ -0,0 +1,11 @@
import java.util.*;
class Foo {
List<String> bar() {
List<String> list = new ArrayList<>();
for (var integer : in<caret>put) {
list.add(integer == null ? null : integer.toString());
}
return Collections.unmodifiableList(list);
}
}

View File

@@ -580,6 +580,17 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
doTest(new MockIntroduceVariableHandler("l", false, false, false, "D<java.lang.Integer>", false));
}
public void testForIterationParameterVar() {
try {
doTest(new MockIntroduceVariableHandler("input", false, false, false, "Object", false));
}
catch (Exception e) {
assertEquals("Error message:Cannot perform refactoring.\nUnknown expression type.", e.getMessage());
return;
}
fail("Should not be able to perform refactoring");
}
public void testOneLineLambdaVoidCompatible() {
doTest(new MockIntroduceVariableHandler("c", false, false, false, CommonClassNames.JAVA_LANG_STRING));
}