Data flow values for Elvis / parenthesized expressions, smart casts on them

This commit is contained in:
Mikhail Glukhikh
2015-12-08 16:50:04 +03:00
parent 745a3aeeac
commit d024045638
10 changed files with 160 additions and 16 deletions
@@ -67,6 +67,27 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
super.visitParameter(parameter);
}
@NotNull
private static PsiElement getSmartCastTarget(@NotNull KtExpression expression) {
PsiElement target = expression;
if (target instanceof KtParenthesizedExpression) {
target = KtPsiUtil.deparenthesize((KtParenthesizedExpression) target);
if (target == null) {
target = expression;
}
}
if (target instanceof KtIfExpression) {
target = ((KtIfExpression) target).getIfKeyword();
}
else if (target instanceof KtWhenExpression) {
target = ((KtWhenExpression) target).getWhenKeyword();
}
else if (target instanceof KtBinaryExpression) {
target = ((KtBinaryExpression) target).getOperationReference();
}
return target;
}
@Override
public void visitExpression(@NotNull KtExpression expression) {
KotlinType implicitSmartCast = bindingContext.get(IMPLICIT_RECEIVER_SMARTCAST, expression);
@@ -84,14 +105,8 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
KotlinType smartCast = bindingContext.get(SMARTCAST, expression);
if (smartCast != null) {
PsiElement target = expression;
if (expression instanceof KtIfExpression) {
target = ((KtIfExpression) expression).getIfKeyword();
}
else if (expression instanceof KtWhenExpression) {
target = ((KtWhenExpression) expression).getWhenKeyword();
}
holder.createInfoAnnotation(target, "Smart cast to " + DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(smartCast))
holder.createInfoAnnotation(getSmartCastTarget(expression),
"Smart cast to " + DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(smartCast))
.setTextAttributes(KotlinHighlightingColors.SMART_CAST_VALUE);
}
+15
View File
@@ -0,0 +1,15 @@
fun foo(s: String) = s.length
fun baz(s: String?, r: String?): Int {
return foo(r <info descr="Smart cast to kotlin.String">?:</info> when {
s != null -> s
else -> ""
})
}
fun bar(s: String?, r: String?): Int {
return (r <info descr="Smart cast to kotlin.String">?:</info> when {
s != null -> s
else -> ""
}).length
}
@@ -724,6 +724,12 @@ public class PsiCheckerTestGenerated extends AbstractPsiCheckerTest {
doTestWithInfos(fileName);
}
@TestMetadata("smartCastOnElvis.kt")
public void testSmartCastOnElvis() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/checker/infos/smartCastOnElvis.kt");
doTestWithInfos(fileName);
}
@TestMetadata("SmartCastOnIf.kt")
public void testSmartCastOnIf() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/checker/infos/SmartCastOnIf.kt");