Report "use of non-const Kotlin property" also on Java case labels
#KT-25536 Fixed
This commit is contained in:
@@ -2788,7 +2788,7 @@
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.FakeJvmFieldConstantInspection"
|
||||
displayName="Kotlin non-const property used as annotation argument"
|
||||
displayName="Kotlin non-const property used as Java constant"
|
||||
groupPath="Kotlin"
|
||||
groupName="Java interop issues"
|
||||
enabledByDefault="true"
|
||||
|
||||
@@ -34,29 +34,40 @@ class FakeJvmFieldConstantInspection : AbstractKotlinInspection() {
|
||||
|
||||
for (attribute in list.attributes) {
|
||||
val valueExpression = attribute.value as? PsiExpression ?: continue
|
||||
val resolvedLightField = (valueExpression as? PsiReference)?.resolve() as? KtLightFieldForDeclaration ?: continue
|
||||
val resolvedProperty = resolvedLightField.kotlinOrigin as? KtProperty ?: continue
|
||||
with(MayBeConstantInspection) {
|
||||
if (resolvedProperty.annotationEntries.isEmpty()) return@with
|
||||
val resolvedPropertyStatus = resolvedProperty.getStatus()
|
||||
if (resolvedPropertyStatus == JVM_FIELD_MIGHT_BE_CONST ||
|
||||
resolvedPropertyStatus == JVM_FIELD_MIGHT_BE_CONST_NO_INITIALIZER ||
|
||||
resolvedPropertyStatus == JVM_FIELD_MIGHT_BE_CONST_ERRONEOUS) {
|
||||
val fixes = mutableListOf<LocalQuickFix>()
|
||||
if (resolvedPropertyStatus == JVM_FIELD_MIGHT_BE_CONST) {
|
||||
fixes += IntentionWrapper(AddConstModifierFix(resolvedProperty), resolvedProperty.containingFile)
|
||||
}
|
||||
holder.registerProblem(
|
||||
valueExpression,
|
||||
"Use of @JvmField non-const Kotlin property as annotation argument is incorrect." +
|
||||
" Will be forbidden in 1.3",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
*fixes.toTypedArray()
|
||||
)
|
||||
}
|
||||
}
|
||||
checkExpression(valueExpression, holder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitSwitchLabelStatement(statement: PsiSwitchLabelStatement) {
|
||||
super.visitSwitchLabelStatement(statement)
|
||||
|
||||
val valueExpression = statement.caseValue ?: return
|
||||
checkExpression(valueExpression, holder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkExpression(valueExpression: PsiExpression, holder: ProblemsHolder) {
|
||||
val resolvedLightField = (valueExpression as? PsiReference)?.resolve() as? KtLightFieldForDeclaration ?: return
|
||||
val resolvedProperty = resolvedLightField.kotlinOrigin as? KtProperty ?: return
|
||||
with(MayBeConstantInspection) {
|
||||
if (resolvedProperty.annotationEntries.isEmpty()) return@with
|
||||
val resolvedPropertyStatus = resolvedProperty.getStatus()
|
||||
if (resolvedPropertyStatus == JVM_FIELD_MIGHT_BE_CONST ||
|
||||
resolvedPropertyStatus == JVM_FIELD_MIGHT_BE_CONST_NO_INITIALIZER ||
|
||||
resolvedPropertyStatus == JVM_FIELD_MIGHT_BE_CONST_ERRONEOUS
|
||||
) {
|
||||
val fixes = mutableListOf<LocalQuickFix>()
|
||||
if (resolvedPropertyStatus == JVM_FIELD_MIGHT_BE_CONST) {
|
||||
fixes += IntentionWrapper(AddConstModifierFix(resolvedProperty), resolvedProperty.containingFile)
|
||||
}
|
||||
holder.registerProblem(
|
||||
valueExpression,
|
||||
"Use of non-const Kotlin property as Java constant is incorrect. Will be forbidden in 1.4",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
*fixes.toTypedArray()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
-1
@@ -9,5 +9,16 @@ public class JavaUser {
|
||||
|
||||
// Safe
|
||||
@Ann(s = KotlinPropertiesKt.constantString)
|
||||
public void baz() {}
|
||||
public void baz(String z) {
|
||||
switch (z) {
|
||||
case KotlinPropertiesKt.constantString: // Safe
|
||||
break;
|
||||
|
||||
case KotlinPropertiesKt.importantString: // Dangerous
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,8 @@
|
||||
<module>First</module>
|
||||
<package><default></package>
|
||||
<entry_point TYPE="file" FQNAME="JavaUser.java" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Kotlin non-const property used as annotation argument</problem_class>
|
||||
<description>Use of @JvmField non-const Kotlin property as annotation argument is incorrect. Will be forbidden in 1.3</description>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Kotlin non-const property used as Java constant</problem_class>
|
||||
<description>Use of non-const Kotlin property as Java constant is incorrect. Will be forbidden in 1.4</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>JavaUser.java</file>
|
||||
@@ -14,7 +14,16 @@
|
||||
<module>First</module>
|
||||
<package><default></package>
|
||||
<entry_point TYPE="file" FQNAME="JavaUser.java" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Kotlin non-const property used as annotation argument</problem_class>
|
||||
<description>Use of @JvmField non-const Kotlin property as annotation argument is incorrect. Will be forbidden in 1.3</description>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Kotlin non-const property used as Java constant</problem_class>
|
||||
<description>Use of non-const Kotlin property as Java constant is incorrect. Will be forbidden in 1.4</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>JavaUser.java</file>
|
||||
<line>17</line>
|
||||
<module>First</module>
|
||||
<package><default></package>
|
||||
<entry_point TYPE="method" FQNAME="JavaUser void baz(java.lang.String z)" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Kotlin non-const property used as Java constant</problem_class>
|
||||
<description>Use of non-const Kotlin property as Java constant is incorrect. Will be forbidden in 1.4</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
Reference in New Issue
Block a user