Has platform type inspection: do not report by default on Kotlin types with platform arguments #KT-13170 Fixed

Also #KT-12820 Obsolete
(cherry picked from commit 0589b48)
This commit is contained in:
Mikhail Glukhikh
2016-07-25 14:05:17 +03:00
committed by Mikhail Glukhikh
parent 8600e7348c
commit ff2f49fa8f
4 changed files with 15 additions and 14 deletions
@@ -34,10 +34,11 @@ import javax.swing.JComponent
class HasPlatformTypeInspection(
val intention: SpecifyTypeExplicitlyIntention = SpecifyTypeExplicitlyIntention(),
@JvmField var publicAPIOnly: Boolean = true
@JvmField var publicAPIOnly: Boolean = true,
@JvmField var reportPlatformArguments: Boolean = false
) : IntentionBasedInspection<KtCallableDeclaration>(
intention,
{ intention.dangerousFlexibleTypeOrNull(it, publicAPIOnly) != null }
{ intention.dangerousFlexibleTypeOrNull(it, publicAPIOnly, reportPlatformArguments) != null }
) {
override val problemHighlightType = ProblemHighlightType.WEAK_WARNING
@@ -45,7 +46,7 @@ class HasPlatformTypeInspection(
override val problemText = "Declaration has platform type. Make the type explicit to prevent subtle bugs."
override fun additionalFixes(element: KtCallableDeclaration): List<LocalQuickFix>? {
val type = intention.dangerousFlexibleTypeOrNull(element, publicAPIOnly) ?: return null
val type = intention.dangerousFlexibleTypeOrNull(element, publicAPIOnly, reportPlatformArguments) ?: return null
if (type.isNullabilityFlexible()) {
val expression = element.node.findChildByType(KtTokens.EQ)?.psi?.getNextSiblingIgnoringWhitespaceAndComments()
@@ -65,6 +66,7 @@ class HasPlatformTypeInspection(
override fun createOptionsPanel(): JComponent? {
val panel = MultipleCheckboxOptionsPanel(this)
panel.addCheckbox("Apply only to public or protected members", "publicAPIOnly")
panel.addCheckbox("Report for types with platform arguments", "reportPlatformArguments")
return panel
}
}
@@ -51,7 +51,9 @@ class SpecifyTypeExplicitlyIntention :
SelfTargetingRangeIntention<KtCallableDeclaration>(KtCallableDeclaration::class.java, "Specify type explicitly"),
LowPriorityAction {
fun dangerousFlexibleTypeOrNull(declaration: KtCallableDeclaration, publicAPIOnly: Boolean): KotlinType? {
fun dangerousFlexibleTypeOrNull(
declaration: KtCallableDeclaration, publicAPIOnly: Boolean, reportPlatformArguments: Boolean
): KotlinType? {
when (declaration) {
is KtFunction -> if (declaration.isLocal || declaration.hasDeclaredReturnType()) return null
is KtProperty -> if (declaration.isLocal || declaration.typeReference != null) return null
@@ -63,7 +65,12 @@ class SpecifyTypeExplicitlyIntention :
val callable = declaration.resolveToDescriptorIfAny() as? CallableDescriptor ?: return null
if (publicAPIOnly && !callable.visibility.isPublicAPI) return null
val type = callable.returnType ?: return null
if (!type.isFlexibleRecursive()) return null
if (reportPlatformArguments) {
if (!type.isFlexibleRecursive()) return null
}
else {
if (!type.isFlexible()) return null
}
return type
}
@@ -23,12 +23,4 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Function, property or variable has platform type.</problem_class>
<description>Declaration has platform type. Make the type explicit to prevent subtle bugs.</description>
</problem>
<problem>
<file>property2.kt</file>
<line>3</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/property2.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Function, property or variable has platform type.</problem_class>
<description>Declaration has platform type. Make the type explicit to prevent subtle bugs.</description>
</problem>
</problems>
+1 -1
View File
@@ -1,3 +1,3 @@
// WITH_RUNTIME
// By default OK: Array<String!>
val list = arrayOf(java.lang.String.valueOf(1))