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"
|
<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"
|
groupPath="Kotlin"
|
||||||
groupName="Java interop issues"
|
groupName="Java interop issues"
|
||||||
enabledByDefault="true"
|
enabledByDefault="true"
|
||||||
|
|||||||
@@ -34,29 +34,40 @@ class FakeJvmFieldConstantInspection : AbstractKotlinInspection() {
|
|||||||
|
|
||||||
for (attribute in list.attributes) {
|
for (attribute in list.attributes) {
|
||||||
val valueExpression = attribute.value as? PsiExpression ?: continue
|
val valueExpression = attribute.value as? PsiExpression ?: continue
|
||||||
val resolvedLightField = (valueExpression as? PsiReference)?.resolve() as? KtLightFieldForDeclaration ?: continue
|
checkExpression(valueExpression, holder)
|
||||||
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()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// Safe
|
||||||
@Ann(s = KotlinPropertiesKt.constantString)
|
@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>
|
<module>First</module>
|
||||||
<package><default></package>
|
<package><default></package>
|
||||||
<entry_point TYPE="file" FQNAME="JavaUser.java" />
|
<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>
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Kotlin non-const property used as Java constant</problem_class>
|
||||||
<description>Use of @JvmField non-const Kotlin property as annotation argument is incorrect. Will be forbidden in 1.3</description>
|
<description>Use of non-const Kotlin property as Java constant is incorrect. Will be forbidden in 1.4</description>
|
||||||
</problem>
|
</problem>
|
||||||
<problem>
|
<problem>
|
||||||
<file>JavaUser.java</file>
|
<file>JavaUser.java</file>
|
||||||
@@ -14,7 +14,16 @@
|
|||||||
<module>First</module>
|
<module>First</module>
|
||||||
<package><default></package>
|
<package><default></package>
|
||||||
<entry_point TYPE="file" FQNAME="JavaUser.java" />
|
<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>
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Kotlin non-const property used as Java constant</problem_class>
|
||||||
<description>Use of @JvmField non-const Kotlin property as annotation argument is incorrect. Will be forbidden in 1.3</description>
|
<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>
|
</problem>
|
||||||
</problems>
|
</problems>
|
||||||
|
|||||||
Reference in New Issue
Block a user