JS: add inspection for the case when something with dynamic type implicitly casted to kotlin another type

This commit is contained in:
Zalim Bashorov
2016-10-10 16:03:56 +03:00
parent d05cb9f894
commit 45d42bdff8
7 changed files with 213 additions and 0 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports expressions with dynamic type in the specified inspection scope that are implicitly casted to another type.
</body>
</html>
+8
View File
@@ -1553,6 +1553,14 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnsafeCastFromDynamicInspection"
displayName="Implicit (unsafe) cast from dynamic type"
groupName="Kotlin"
enabledByDefault="true"
level="INFO"
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.OverridingDeprecatedMemberInspection"
shortName="OverridingDeprecatedMember"
displayName="Overriding deprecated member"
@@ -0,0 +1,47 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInspection.LocalInspectionToolSession
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtVisitorVoid
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.isDynamic
class UnsafeCastFromDynamicInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
return object : KtVisitorVoid() {
override fun visitExpression(expression: KtExpression) {
super.visitExpression(expression)
val context = expression.analyze(BodyResolveMode.PARTIAL)
val expectedType = context[BindingContext.EXPECTED_EXPRESSION_TYPE, expression] ?: return
val actualType = expression.getType(context) ?: return
if (actualType.isDynamic() && !expectedType.isDynamic() && !TypeUtils.noExpectedType(expectedType)) {
holder.registerProblem(expression, "Implicit (unsafe) cast from dynamic to $expectedType")
}
}
}
}
}
@@ -0,0 +1,98 @@
<problems>
<problem>
<file>test.kt</file>
<line>13</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Implicit (unsafe) cast from dynamic type</problem_class>
<description>Implicit (unsafe) cast from dynamic to String</description>
</problem>
<problem>
<file>test.kt</file>
<line>14</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Implicit (unsafe) cast from dynamic type</problem_class>
<description>Implicit (unsafe) cast from dynamic to Any</description>
</problem>
<problem>
<file>test.kt</file>
<line>15</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Implicit (unsafe) cast from dynamic type</problem_class>
<description>Implicit (unsafe) cast from dynamic to Unit</description>
</problem>
<problem>
<file>test.kt</file>
<line>22</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Implicit (unsafe) cast from dynamic type</problem_class>
<description>Implicit (unsafe) cast from dynamic to String</description>
</problem>
<problem>
<file>test.kt</file>
<line>36</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Implicit (unsafe) cast from dynamic type</problem_class>
<description>Implicit (unsafe) cast from dynamic to Any</description>
</problem>
<problem>
<file>test.kt</file>
<line>40</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Implicit (unsafe) cast from dynamic type</problem_class>
<description>Implicit (unsafe) cast from dynamic to Unit</description>
</problem>
<problem>
<file>test.kt</file>
<line>47</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Implicit (unsafe) cast from dynamic type</problem_class>
<description>Implicit (unsafe) cast from dynamic to Unit</description>
</problem>
<problem>
<file>test.kt</file>
<line>43</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Implicit (unsafe) cast from dynamic type</problem_class>
<description>Implicit (unsafe) cast from dynamic to String</description>
</problem>
<problem>
<file>test.kt</file>
<line>43</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Implicit (unsafe) cast from dynamic type</problem_class>
<description>Implicit (unsafe) cast from dynamic to Any</description>
</problem>
<problem>
<file>test.kt</file>
<line>43</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Implicit (unsafe) cast from dynamic type</problem_class>
<description>Implicit (unsafe) cast from dynamic to Unit</description>
</problem>
<problem>
<file>test.kt</file>
<line>45</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Implicit (unsafe) cast from dynamic type</problem_class>
<description>Implicit (unsafe) cast from dynamic to Any</description>
</problem>
<problem>
<file>test.kt</file>
<line>45</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Implicit (unsafe) cast from dynamic type</problem_class>
<description>Implicit (unsafe) cast from dynamic to Unit</description>
</problem>
</problems>
@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.UnsafeCastFromDynamicInspection
+48
View File
@@ -0,0 +1,48 @@
class A {
fun foo(i: Int) {}
}
fun bar(d: dynamic, s: String, a: Any, u: Unit) {}
fun main(args: Array<String>) {
val d: dynamic = Any()
var s: String = ""
var a: Any = ""
var u: Unit = Unit
s = d
a = d
u = d
s = d as String
a = d as Any
u = d as Unit
if (d is String) {
s = d
d.subSequence(1, 2)
}
if (d is A) {
d.foo(1)
}
if (a is String) {
s = a
a.length
}
if (d is Any) {
a = d
}
if (d is Unit) {
u = d
}
bar(d, d.boo, d, d)
bar(d, d as String, d as Any, d as Unit)
bar(d.aaa, d.bbb as String, d.ccc(), d.ddd {})
return d
}
@@ -160,6 +160,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
doTest(fileName);
}
@TestMetadata("dynamic/js/inspectionData/inspections.test")
public void testDynamic_js_inspectionData_Inspections_test() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/dynamic/js/inspectionData/inspections.test");
doTest(fileName);
}
@TestMetadata("equalsAndHashCode/inspectionData/inspections.test")
public void testEqualsAndHashCode_inspectionData_Inspections_test() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/equalsAndHashCode/inspectionData/inspections.test");