Don't suggest might be const on actual member declaration
#KT-27822 Fixed
This commit is contained in:
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.psi.KtObjectDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.psi.propertyVisitor
|
||||
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
|
||||
import org.jetbrains.kotlin.psi.psiUtil.hasActualModifier
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isObjectLiteral
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.constants.ErrorValue
|
||||
@@ -40,8 +41,7 @@ class MayBeConstantInspection : AbstractKotlinInspection() {
|
||||
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return propertyVisitor { property ->
|
||||
val status = property.getStatus()
|
||||
when (status) {
|
||||
when (val status = property.getStatus()) {
|
||||
NONE, JVM_FIELD_MIGHT_BE_CONST_NO_INITIALIZER,
|
||||
MIGHT_BE_CONST_ERRONEOUS, JVM_FIELD_MIGHT_BE_CONST_ERRONEOUS -> return@propertyVisitor
|
||||
MIGHT_BE_CONST, JVM_FIELD_MIGHT_BE_CONST -> {
|
||||
@@ -59,7 +59,7 @@ class MayBeConstantInspection : AbstractKotlinInspection() {
|
||||
companion object {
|
||||
fun KtProperty.getStatus(): Status {
|
||||
if (isLocal || isVar || getter != null ||
|
||||
hasModifier(KtTokens.CONST_KEYWORD) || hasModifier(KtTokens.OVERRIDE_KEYWORD)
|
||||
hasModifier(KtTokens.CONST_KEYWORD) || hasModifier(KtTokens.OVERRIDE_KEYWORD) || hasActualModifier()
|
||||
) {
|
||||
return NONE
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import org.jetbrains.kotlin.psi.KtAnnotationEntry
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.psi.KtReferenceExpression
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.hasActualModifier
|
||||
import org.jetbrains.kotlin.resolve.checkers.ConstModifierChecker
|
||||
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
|
||||
|
||||
@@ -69,11 +70,15 @@ class AddConstModifierIntention : SelfTargetingIntention<KtProperty>(KtProperty:
|
||||
|
||||
companion object {
|
||||
fun isApplicableTo(element: KtProperty): Boolean {
|
||||
if (element.isLocal || element.isVar || element.hasDelegate() || element.initializer == null
|
||||
|| element.getter?.hasBody() == true || element.receiverTypeReference != null
|
||||
|| element.hasModifier(KtTokens.CONST_KEYWORD) || element.hasModifier(KtTokens.OVERRIDE_KEYWORD)) {
|
||||
return false
|
||||
with(element) {
|
||||
if (isLocal || isVar || hasDelegate() || initializer == null
|
||||
|| getter?.hasBody() == true || receiverTypeReference != null
|
||||
|| hasModifier(KtTokens.CONST_KEYWORD) || hasModifier(KtTokens.OVERRIDE_KEYWORD) || hasActualModifier()
|
||||
) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
val propertyDescriptor = element.descriptor as? VariableDescriptor ?: return false
|
||||
return ConstModifierChecker.canBeConst(element, element, propertyDescriptor)
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
expect val x: Int
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Make 'x' const" "false"
|
||||
// ACTION: Add getter
|
||||
// ACTION: Convert property initializer to getter
|
||||
// ACTION: Convert property to function
|
||||
// ACTION: Specify type explicitly
|
||||
|
||||
actual val x<caret> = 42
|
||||
+5
@@ -549,6 +549,11 @@ public class QuickFixMultiModuleTestGenerated extends AbstractQuickFixMultiModul
|
||||
runTest("idea/testData/multiModuleQuickFix/makeOpenFromExpect/");
|
||||
}
|
||||
|
||||
@TestMetadata("mayBeConstantWithActual")
|
||||
public void testMayBeConstantWithActual() throws Exception {
|
||||
runTest("idea/testData/multiModuleQuickFix/mayBeConstantWithActual/");
|
||||
}
|
||||
|
||||
@TestMetadata("memberFunParameterToReceiverByHeader")
|
||||
public void testMemberFunParameterToReceiverByHeader() throws Exception {
|
||||
runTest("idea/testData/multiModuleQuickFix/memberFunParameterToReceiverByHeader/");
|
||||
|
||||
Reference in New Issue
Block a user