mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
inline: insert cast in foreach stmts when var/initializer was casted with explicit cast conversion (IDEA-73501)
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
package com.intellij.refactoring.util;
|
||||
|
||||
import com.intellij.codeInsight.ChangeContextUtil;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.GenericsHighlightUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.progress.ProgressManager;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
@@ -48,6 +49,7 @@ public class InlineUtil {
|
||||
|
||||
PsiClass thisClass = RefactoringUtil.getThisClass(initializer);
|
||||
PsiClass refParent = RefactoringUtil.getThisClass(ref);
|
||||
boolean insertCastWhenUnchecked = ref.getParent() instanceof PsiForeachStatement;
|
||||
final PsiType varType = variable.getType();
|
||||
initializer = RefactoringUtil.convertInitializerToNormalExpression(initializer, varType);
|
||||
|
||||
@@ -56,6 +58,7 @@ public class InlineUtil {
|
||||
PsiType exprType = expr.getType();
|
||||
if (exprType != null && (!varType.equals(exprType) && varType instanceof PsiPrimitiveType
|
||||
|| !TypeConversionUtil.isAssignable(varType, exprType)
|
||||
|| insertCastWhenUnchecked && GenericsHighlightUtil.isRawToGeneric(varType, exprType)
|
||||
|| expr instanceof PsiConditionalExpression)) {
|
||||
boolean matchedTypes = false;
|
||||
//try explicit type arguments
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import java.util.*;
|
||||
|
||||
class X {
|
||||
List getChildren() {
|
||||
return null;
|
||||
}
|
||||
|
||||
void iterate() {
|
||||
List<X> xs = getChildren();
|
||||
for (X x : x<caret>s) {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import java.util.*;
|
||||
|
||||
class X {
|
||||
List getChildren() {
|
||||
return null;
|
||||
}
|
||||
|
||||
void iterate() {
|
||||
for (X x : (List<X>) getChildren()) {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import java.util.*;
|
||||
|
||||
class X {
|
||||
List getChildren() {
|
||||
return null;
|
||||
}
|
||||
|
||||
void iterate() {
|
||||
List<X> xs = getChildren();
|
||||
foo(x<caret>s);
|
||||
}
|
||||
void foo(List<X> l){}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import java.util.*;
|
||||
|
||||
class X {
|
||||
List getChildren() {
|
||||
return null;
|
||||
}
|
||||
|
||||
void iterate() {
|
||||
foo(getChildren());
|
||||
}
|
||||
void foo(List<X> l){}
|
||||
}
|
||||
@@ -192,6 +192,13 @@ public class InlineLocalTest extends LightCodeInsightTestCase {
|
||||
doTest(true);
|
||||
}
|
||||
|
||||
public void testUncheckedCast() throws Exception {
|
||||
doTest(true);
|
||||
}
|
||||
public void testUncheckedCastNotNeeded() throws Exception {
|
||||
doTest(true);
|
||||
}
|
||||
|
||||
public void testLocalVarInsideLambdaBodyWriteUsage() throws Exception {
|
||||
doTest(true, "Cannot perform refactoring.\n" +
|
||||
"Variable 'hello' is accessed for writing.");
|
||||
|
||||
Reference in New Issue
Block a user