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:
committed by
Mikhail Glukhikh
parent
8600e7348c
commit
ff2f49fa8f
@@ -34,10 +34,11 @@ import javax.swing.JComponent
|
|||||||
|
|
||||||
class HasPlatformTypeInspection(
|
class HasPlatformTypeInspection(
|
||||||
val intention: SpecifyTypeExplicitlyIntention = SpecifyTypeExplicitlyIntention(),
|
val intention: SpecifyTypeExplicitlyIntention = SpecifyTypeExplicitlyIntention(),
|
||||||
@JvmField var publicAPIOnly: Boolean = true
|
@JvmField var publicAPIOnly: Boolean = true,
|
||||||
|
@JvmField var reportPlatformArguments: Boolean = false
|
||||||
) : IntentionBasedInspection<KtCallableDeclaration>(
|
) : IntentionBasedInspection<KtCallableDeclaration>(
|
||||||
intention,
|
intention,
|
||||||
{ intention.dangerousFlexibleTypeOrNull(it, publicAPIOnly) != null }
|
{ intention.dangerousFlexibleTypeOrNull(it, publicAPIOnly, reportPlatformArguments) != null }
|
||||||
) {
|
) {
|
||||||
|
|
||||||
override val problemHighlightType = ProblemHighlightType.WEAK_WARNING
|
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 val problemText = "Declaration has platform type. Make the type explicit to prevent subtle bugs."
|
||||||
|
|
||||||
override fun additionalFixes(element: KtCallableDeclaration): List<LocalQuickFix>? {
|
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()) {
|
if (type.isNullabilityFlexible()) {
|
||||||
val expression = element.node.findChildByType(KtTokens.EQ)?.psi?.getNextSiblingIgnoringWhitespaceAndComments()
|
val expression = element.node.findChildByType(KtTokens.EQ)?.psi?.getNextSiblingIgnoringWhitespaceAndComments()
|
||||||
@@ -65,6 +66,7 @@ class HasPlatformTypeInspection(
|
|||||||
override fun createOptionsPanel(): JComponent? {
|
override fun createOptionsPanel(): JComponent? {
|
||||||
val panel = MultipleCheckboxOptionsPanel(this)
|
val panel = MultipleCheckboxOptionsPanel(this)
|
||||||
panel.addCheckbox("Apply only to public or protected members", "publicAPIOnly")
|
panel.addCheckbox("Apply only to public or protected members", "publicAPIOnly")
|
||||||
|
panel.addCheckbox("Report for types with platform arguments", "reportPlatformArguments")
|
||||||
return panel
|
return panel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,9 @@ class SpecifyTypeExplicitlyIntention :
|
|||||||
SelfTargetingRangeIntention<KtCallableDeclaration>(KtCallableDeclaration::class.java, "Specify type explicitly"),
|
SelfTargetingRangeIntention<KtCallableDeclaration>(KtCallableDeclaration::class.java, "Specify type explicitly"),
|
||||||
LowPriorityAction {
|
LowPriorityAction {
|
||||||
|
|
||||||
fun dangerousFlexibleTypeOrNull(declaration: KtCallableDeclaration, publicAPIOnly: Boolean): KotlinType? {
|
fun dangerousFlexibleTypeOrNull(
|
||||||
|
declaration: KtCallableDeclaration, publicAPIOnly: Boolean, reportPlatformArguments: Boolean
|
||||||
|
): KotlinType? {
|
||||||
when (declaration) {
|
when (declaration) {
|
||||||
is KtFunction -> if (declaration.isLocal || declaration.hasDeclaredReturnType()) return null
|
is KtFunction -> if (declaration.isLocal || declaration.hasDeclaredReturnType()) return null
|
||||||
is KtProperty -> if (declaration.isLocal || declaration.typeReference != null) 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
|
val callable = declaration.resolveToDescriptorIfAny() as? CallableDescriptor ?: return null
|
||||||
if (publicAPIOnly && !callable.visibility.isPublicAPI) return null
|
if (publicAPIOnly && !callable.visibility.isPublicAPI) return null
|
||||||
val type = callable.returnType ?: 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
|
return type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,12 +23,4 @@
|
|||||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Function, property or variable has platform type.</problem_class>
|
<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>
|
<description>Declaration has platform type. Make the type explicit to prevent subtle bugs.</description>
|
||||||
</problem>
|
</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>
|
</problems>
|
||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
// By default OK: Array<String!>
|
||||||
val list = arrayOf(java.lang.String.valueOf(1))
|
val list = arrayOf(java.lang.String.valueOf(1))
|
||||||
Reference in New Issue
Block a user