From 58b815d8423464d9f5402349d984a53497035143 Mon Sep 17 00:00:00 2001 From: Dmitry Savvinov Date: Tue, 17 Oct 2017 13:46:18 +0300 Subject: [PATCH] Fix false USELESS_CAST in function with expression body Note that current behaviour is made similar to the case with properties initializers/accessors, which means that more complex cases are not covered yet (see KT-20801) #KT-20802 fixed. --- .../jetbrains/kotlin/types/CastDiagnosticsUtil.kt | 5 +++-- .../diagnostics/tests/cast/AsInExpressionBody.kt | 12 ++++++++++++ .../diagnostics/tests/cast/AsInExpressionBody.txt | 10 ++++++++++ .../testData/diagnostics/tests/cast/nothingAs.kt | 12 ++++++------ .../kotlin/checkers/DiagnosticsTestGenerated.java | 6 ++++++ .../javac/DiagnosticsUsingJavacTestGenerated.java | 6 ++++++ 6 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/cast/AsInExpressionBody.kt create mode 100644 compiler/testData/diagnostics/tests/cast/AsInExpressionBody.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt index faa4e85e65c..399a1ec50ed 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt @@ -272,8 +272,9 @@ object CastDiagnosticsUtil { // in unary expression, left argument can be a receiver is KtBinaryExpression, is KtUnaryExpression -> true - // Previously we've checked that there is no expected type, therefore cast in property has an effect on inference - is KtProperty, is KtPropertyAccessor -> true + // Previously we've checked that there is no expected type, therefore cast in property or + // in function has an effect on inference and thus isn't useless + is KtProperty, is KtPropertyAccessor, is KtNamedFunction, is KtFunctionLiteral -> true else -> false } diff --git a/compiler/testData/diagnostics/tests/cast/AsInExpressionBody.kt b/compiler/testData/diagnostics/tests/cast/AsInExpressionBody.kt new file mode 100644 index 00000000000..0f1db40c46a --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/AsInExpressionBody.kt @@ -0,0 +1,12 @@ +fun id(x: T): T = x + +fun foo() = 1 as Any +fun bar() = id(1) as Any +fun baz() = (1 + 1) as Any + +val functionLiteral1 = fun() = 1 as Any +val functionLiteral2 = fun() = id(1) as Any +val functionLiteral3 = fun() = (1 + 1) as Any + +// TODO: this and more complex cases are not supported yet +fun baz(b: Boolean) = if (b) 1 as Any else 42 as Any? \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/cast/AsInExpressionBody.txt b/compiler/testData/diagnostics/tests/cast/AsInExpressionBody.txt new file mode 100644 index 00000000000..906ff51bb48 --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/AsInExpressionBody.txt @@ -0,0 +1,10 @@ +package + +public val functionLiteral1: () -> kotlin.Any +public val functionLiteral2: () -> kotlin.Any +public val functionLiteral3: () -> kotlin.Any +public fun bar(): kotlin.Any +public fun baz(): kotlin.Any +public fun baz(/*0*/ b: kotlin.Boolean): kotlin.Any? +public fun foo(): kotlin.Any +public fun id(/*0*/ x: T): T diff --git a/compiler/testData/diagnostics/tests/cast/nothingAs.kt b/compiler/testData/diagnostics/tests/cast/nothingAs.kt index 404a0678045..fcf008f09fc 100644 --- a/compiler/testData/diagnostics/tests/cast/nothingAs.kt +++ b/compiler/testData/diagnostics/tests/cast/nothingAs.kt @@ -23,9 +23,9 @@ fun test() { val f = TODO() as Interface } -fun a() = TODO() as Any -fun b() = TODO() as Any? -fun c() = TODO() as OpenClass -fun d() = TODO() as FinalClass -fun e() = TODO() as AbstractClass -fun f() = TODO() as Interface +fun a() = TODO() as Any +fun b() = TODO() as Any? +fun c() = TODO() as OpenClass +fun d() = TODO() as FinalClass +fun e() = TODO() as AbstractClass +fun f() = TODO() as Interface diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 1ace858be82..7360f15d678 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -2776,6 +2776,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("AsInExpressionBody.kt") + public void testAsInExpressionBody() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsInExpressionBody.kt"); + doTest(fileName); + } + @TestMetadata("AsInPropertyAndPropertyAccessor.kt") public void testAsInPropertyAndPropertyAccessor() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsInPropertyAndPropertyAccessor.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index e0eb59408e5..ae4afc91801 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -2776,6 +2776,12 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing doTest(fileName); } + @TestMetadata("AsInExpressionBody.kt") + public void testAsInExpressionBody() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsInExpressionBody.kt"); + doTest(fileName); + } + @TestMetadata("AsInPropertyAndPropertyAccessor.kt") public void testAsInPropertyAndPropertyAccessor() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsInPropertyAndPropertyAccessor.kt");