inline: do not insert casts when inline conditional expression in statements (IDEA-97902)

This commit is contained in:
anna
2012-12-27 18:26:07 +01:00
parent 9b3b277fd9
commit c66e1c07b8
4 changed files with 36 additions and 3 deletions

View File

@@ -56,10 +56,9 @@ public class InlineUtil {
ChangeContextUtil.encodeContextInfo(initializer, false);
PsiExpression expr = (PsiExpression)replaceDiamondWithInferredTypesIfNeeded(initializer, ref);
PsiType exprType = expr.getType();
if (exprType != null && (!varType.equals(exprType) && varType instanceof PsiPrimitiveType
if (exprType != null && (!varType.equals(exprType) && (varType instanceof PsiPrimitiveType || exprType instanceof PsiPrimitiveType)
|| !TypeConversionUtil.isAssignable(varType, exprType)
|| insertCastWhenUnchecked && GenericsHighlightUtil.isRawToGeneric(varType, exprType)
|| expr instanceof PsiConditionalExpression)) {
|| insertCastWhenUnchecked && GenericsHighlightUtil.isRawToGeneric(varType, exprType))) {
boolean matchedTypes = false;
//try explicit type arguments
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();

View File

@@ -0,0 +1,15 @@
class Test {
static boolean isA() {
return true;
}
static boolean isB() {
return true;
}
private static void foo(final boolean explicit) {
final boolean has = explicit ? isA() : isB();
if (ha<caret>s) {
}
}
}

View File

@@ -0,0 +1,14 @@
class Test {
static boolean isA() {
return true;
}
static boolean isB() {
return true;
}
private static void foo(final boolean explicit) {
if (explicit ? isA() : isB()) {
}
}
}

View File

@@ -195,10 +195,15 @@ public class InlineLocalTest extends LightCodeInsightTestCase {
public void testUncheckedCast() throws Exception {
doTest(true);
}
public void testUncheckedCastNotNeeded() throws Exception {
doTest(true);
}
public void testCastNotNeeded() throws Exception {
doTest(true);
}
public void testLocalVarInsideLambdaBodyWriteUsage() throws Exception {
doTest(true, "Cannot perform refactoring.\n" +
"Variable 'hello' is accessed for writing.");