diff --git a/idea/src/META-INF/plugin-common.xml b/idea/src/META-INF/plugin-common.xml index c7b1ddaa2ad..3b7af635336 100644 --- a/idea/src/META-INF/plugin-common.xml +++ b/idea/src/META-INF/plugin-common.xml @@ -2788,7 +2788,7 @@ /> () - 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() + 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() + ) + } } } } \ No newline at end of file diff --git a/idea/testData/multiFileInspections/fakeJvmFieldConstant/before/First/src/JavaUser.java b/idea/testData/multiFileInspections/fakeJvmFieldConstant/before/First/src/JavaUser.java index 10506a95635..0507ce3cceb 100644 --- a/idea/testData/multiFileInspections/fakeJvmFieldConstant/before/First/src/JavaUser.java +++ b/idea/testData/multiFileInspections/fakeJvmFieldConstant/before/First/src/JavaUser.java @@ -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; + } + } } \ No newline at end of file diff --git a/idea/testData/multiFileInspections/fakeJvmFieldConstant/expected.xml b/idea/testData/multiFileInspections/fakeJvmFieldConstant/expected.xml index a8e9c4655f2..c1266c5029a 100644 --- a/idea/testData/multiFileInspections/fakeJvmFieldConstant/expected.xml +++ b/idea/testData/multiFileInspections/fakeJvmFieldConstant/expected.xml @@ -5,8 +5,8 @@ First <default> - Kotlin non-const property used as annotation argument - Use of @JvmField non-const Kotlin property as annotation argument is incorrect. Will be forbidden in 1.3 + Kotlin non-const property used as Java constant + Use of non-const Kotlin property as Java constant is incorrect. Will be forbidden in 1.4 JavaUser.java @@ -14,7 +14,16 @@ First <default> - Kotlin non-const property used as annotation argument - Use of @JvmField non-const Kotlin property as annotation argument is incorrect. Will be forbidden in 1.3 + Kotlin non-const property used as Java constant + Use of non-const Kotlin property as Java constant is incorrect. Will be forbidden in 1.4 + + + JavaUser.java + 17 + First + <default> + + Kotlin non-const property used as Java constant + Use of non-const Kotlin property as Java constant is incorrect. Will be forbidden in 1.4