Fix false warning about useless cast in property and property accessor

Note that there are some other problems, for example:
`val a = if (true) 1 as Number else 2`, here we'll get useless cast

 #KT-9551 Fixed
 #KT-9645 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-04-05 04:59:16 +03:00
parent 12db3a263e
commit 13eddba1f2
7 changed files with 36 additions and 3 deletions
@@ -445,6 +445,10 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
if (parent instanceof KtBinaryExpression || parent instanceof KtUnaryExpression) {
return true;
}
// Previously we've checked that there is no expected type, therefore cast in property has an effect on inference
if (parent instanceof KtProperty || parent instanceof KtPropertyAccessor) {
return true;
}
return false;
}
+1 -1
View File
@@ -15,7 +15,7 @@ fun test() : Unit {
checkSubtype<Int?>(x <!USELESS_CAST!>as? Int?<!>)
checkSubtype<Int?>(y as? Int?)
val <!UNUSED_VARIABLE!>s<!> = "" <!USELESS_CAST!>as Any<!>
val <!UNUSED_VARIABLE!>s<!> = "" as Any
("" as String?)?.length
(data@("" as String?))?.length
(<!WRONG_ANNOTATION_TARGET!>@MustBeDocumented()<!>( "" as String?))?.length
@@ -0,0 +1,17 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE
fun test() {
val a = 1 as Any?
val b: Number = 1 <!USELESS_CAST!>as Number<!>
val c = null as String?
val d: Number = 1 <!USELESS_CAST!>as Int<!>
}
val c1 get() = 1 as Number
val c2: Number get() = 1 <!USELESS_CAST!>as Number<!>
val d: Number
get() {
1 <!USELESS_CAST!>as Number<!>
return 1 <!USELESS_CAST!>as Number<!>
}
@@ -0,0 +1,6 @@
package
public val c1: kotlin.Number
public val c2: kotlin.Number
public val d: kotlin.Number
public fun test(): kotlin.Unit
+1 -1
View File
@@ -1,7 +1,7 @@
typealias MyString = String
val x: MyString = ""
val y = x <!USELESS_CAST!>as Any<!>
val y = x as Any
interface Base
class Derived : Base
@@ -3,6 +3,6 @@
class G<T>
fun foo(p: <!UNRESOLVED_REFERENCE!>P<!>) {
val v = <!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>p<!> <!USELESS_CAST!>as <!NO_TYPE_ARGUMENTS_ON_RHS!>G?<!><!>
val v = <!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>p<!> as <!NO_TYPE_ARGUMENTS_ON_RHS!>G?<!>
checkSubtype<G<*>>(v!!)
}
@@ -2686,6 +2686,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("AsInPropertyAndPropertyAccessor.kt")
public void testAsInPropertyAndPropertyAccessor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsInPropertyAndPropertyAccessor.kt");
doTest(fileName);
}
@TestMetadata("AsNothing.kt")
public void testAsNothing() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsNothing.kt");