add quickfix for NON_CONST_VAL_USED_IN_CONSTANT_EXPRESSION
This commit is contained in:
@@ -16,11 +16,14 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.resolve
|
package org.jetbrains.kotlin.resolve
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
|
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
|
||||||
import org.jetbrains.kotlin.diagnostics.Errors
|
import org.jetbrains.kotlin.diagnostics.Errors
|
||||||
import org.jetbrains.kotlin.lexer.JetTokens
|
import org.jetbrains.kotlin.lexer.JetTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.JetDeclaration
|
||||||
|
import org.jetbrains.kotlin.psi.JetProperty
|
||||||
|
|
||||||
public object ConstModifierChecker : DeclarationChecker {
|
public object ConstModifierChecker : DeclarationChecker {
|
||||||
override fun check(
|
override fun check(
|
||||||
@@ -31,44 +34,48 @@ public object ConstModifierChecker : DeclarationChecker {
|
|||||||
) {
|
) {
|
||||||
if (descriptor !is VariableDescriptor || !declaration.hasModifier(JetTokens.CONST_KEYWORD)) return
|
if (descriptor !is VariableDescriptor || !declaration.hasModifier(JetTokens.CONST_KEYWORD)) return
|
||||||
|
|
||||||
val containingDeclaration = descriptor.containingDeclaration
|
|
||||||
val constModifierPsiElement = declaration.modifierList!!.getModifier(JetTokens.CONST_KEYWORD)!!
|
val constModifierPsiElement = declaration.modifierList!!.getModifier(JetTokens.CONST_KEYWORD)!!
|
||||||
|
|
||||||
|
val diagnostic = checkCanBeConst(declaration, constModifierPsiElement, descriptor)
|
||||||
|
if (diagnostic != null) {
|
||||||
|
diagnosticHolder.report(diagnostic)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun checkCanBeConst(declaration: JetDeclaration,
|
||||||
|
constModifierPsiElement: PsiElement,
|
||||||
|
descriptor: VariableDescriptor): Diagnostic? {
|
||||||
if (descriptor.isVar) {
|
if (descriptor.isVar) {
|
||||||
diagnosticHolder.report(Errors.WRONG_MODIFIER_TARGET.on(constModifierPsiElement, JetTokens.CONST_KEYWORD, "vars"))
|
return Errors.WRONG_MODIFIER_TARGET.on(constModifierPsiElement, JetTokens.CONST_KEYWORD, "vars")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val containingDeclaration = descriptor.containingDeclaration
|
||||||
if (containingDeclaration is ClassDescriptor && containingDeclaration.kind != ClassKind.OBJECT) {
|
if (containingDeclaration is ClassDescriptor && containingDeclaration.kind != ClassKind.OBJECT) {
|
||||||
diagnosticHolder.report(Errors.CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT.on(constModifierPsiElement))
|
return Errors.CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT.on(constModifierPsiElement)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (declaration !is JetProperty || descriptor !is PropertyDescriptor) return
|
if (declaration !is JetProperty || descriptor !is PropertyDescriptor) return null
|
||||||
|
|
||||||
if (declaration.hasDelegate()) {
|
if (declaration.hasDelegate()) {
|
||||||
diagnosticHolder.report(Errors.CONST_VAL_WITH_DELEGATE.on(declaration.delegate!!))
|
return Errors.CONST_VAL_WITH_DELEGATE.on(declaration.delegate!!)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (descriptor is PropertyDescriptor && !descriptor.getter!!.isDefault) {
|
if (descriptor is PropertyDescriptor && !descriptor.getter!!.isDefault) {
|
||||||
diagnosticHolder.report(Errors.CONST_VAL_WITH_GETTER.on(declaration.getter!!))
|
return Errors.CONST_VAL_WITH_GETTER.on(declaration.getter!!)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!descriptor.type.canBeUsedForConstVal()) {
|
if (!descriptor.type.canBeUsedForConstVal()) {
|
||||||
diagnosticHolder.report(Errors.TYPE_CANT_BE_USED_FOR_CONST_VAL.on(constModifierPsiElement, descriptor.type))
|
return Errors.TYPE_CANT_BE_USED_FOR_CONST_VAL.on(constModifierPsiElement, descriptor.type)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (declaration.initializer == null) {
|
if (declaration.initializer == null) {
|
||||||
diagnosticHolder.report(Errors.CONST_VAL_WITHOUT_INITIALIZER.on(constModifierPsiElement))
|
return Errors.CONST_VAL_WITHOUT_INITIALIZER.on(constModifierPsiElement)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (descriptor.compileTimeInitializer == null) {
|
if (descriptor.compileTimeInitializer == null) {
|
||||||
diagnosticHolder.report(Errors.CONST_VAL_WITH_NON_CONST_INITIALIZER.on(declaration.initializer!!))
|
return Errors.CONST_VAL_WITH_NON_CONST_INITIALIZER.on(declaration.initializer!!)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe
|
|||||||
ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION,
|
ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION,
|
||||||
Errors.DEPRECATED_SYMBOL_WITH_MESSAGE,
|
Errors.DEPRECATED_SYMBOL_WITH_MESSAGE,
|
||||||
Errors.ACCESS_TO_PRIVATE_TOP_LEVEL_FROM_ANOTHER_FILE,
|
Errors.ACCESS_TO_PRIVATE_TOP_LEVEL_FROM_ANOTHER_FILE,
|
||||||
|
Errors.NON_CONST_VAL_USED_IN_CONSTANT_EXPRESSION,
|
||||||
Errors.BACKING_FIELD_SYNTAX_DEPRECATED
|
Errors.BACKING_FIELD_SYNTAX_DEPRECATED
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ import static org.jetbrains.kotlin.lexer.JetTokens.ABSTRACT_KEYWORD;
|
|||||||
public class AddModifierFix extends JetIntentionAction<JetModifierListOwner> {
|
public class AddModifierFix extends JetIntentionAction<JetModifierListOwner> {
|
||||||
@NotNull private final JetModifierKeywordToken modifier;
|
@NotNull private final JetModifierKeywordToken modifier;
|
||||||
|
|
||||||
private AddModifierFix(@NotNull JetModifierListOwner element, @NotNull JetModifierKeywordToken modifier) {
|
public AddModifierFix(@NotNull JetModifierListOwner element, @NotNull JetModifierKeywordToken modifier) {
|
||||||
super(element);
|
super(element);
|
||||||
this.modifier = modifier;
|
this.modifier = modifier;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2015 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.quickfix
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.intention.IntentionAction
|
||||||
|
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||||
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
|
import org.jetbrains.kotlin.lexer.JetTokens
|
||||||
|
import org.jetbrains.kotlin.psi.JetDeclaration
|
||||||
|
import org.jetbrains.kotlin.psi.JetReferenceExpression
|
||||||
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
|
import org.jetbrains.kotlin.resolve.ConstModifierChecker
|
||||||
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
|
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
|
||||||
|
|
||||||
|
public object ConstFixFactory : JetSingleIntentionActionFactory() {
|
||||||
|
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||||
|
val expr = diagnostic.psiElement as? JetReferenceExpression ?: return null
|
||||||
|
val bindingContext = expr.analyze(BodyResolveMode.PARTIAL)
|
||||||
|
val targetDescriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, expr) as? VariableDescriptor ?: return null
|
||||||
|
val declaration = (targetDescriptor.source as? PsiSourceElement)?.psi as? JetDeclaration ?: return null
|
||||||
|
if (ConstModifierChecker.checkCanBeConst(declaration, declaration, targetDescriptor) == null) {
|
||||||
|
return object : AddModifierFix(declaration, JetTokens.CONST_KEYWORD), CleanupFix {}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -321,6 +321,8 @@ public class QuickFixRegistrar : QuickFixContributor {
|
|||||||
UPPER_BOUND_VIOLATED.registerFactory(AddGenericUpperBoundFix.Factory)
|
UPPER_BOUND_VIOLATED.registerFactory(AddGenericUpperBoundFix.Factory)
|
||||||
TYPE_INFERENCE_UPPER_BOUND_VIOLATED.registerFactory(AddGenericUpperBoundFix.Factory)
|
TYPE_INFERENCE_UPPER_BOUND_VIOLATED.registerFactory(AddGenericUpperBoundFix.Factory)
|
||||||
|
|
||||||
|
NON_CONST_VAL_USED_IN_CONSTANT_EXPRESSION.registerFactory(ConstFixFactory)
|
||||||
|
|
||||||
BACKING_FIELD_SYNTAX_DEPRECATED.registerFactory(MigrateBackingFieldSyntaxFix)
|
BACKING_FIELD_SYNTAX_DEPRECATED.registerFactory(MigrateBackingFieldSyntaxFix)
|
||||||
BACKING_FIELD_USAGE_DEPRECATED.registerFactory(MigrateBackingFieldUsageFix)
|
BACKING_FIELD_USAGE_DEPRECATED.registerFactory(MigrateBackingFieldUsageFix)
|
||||||
BACKING_FIELD_USAGE_DEPRECATED.registerFactory(IntroduceBackingPropertyFix)
|
BACKING_FIELD_USAGE_DEPRECATED.registerFactory(IntroduceBackingPropertyFix)
|
||||||
|
|||||||
@@ -36,6 +36,12 @@ fun unnecessaryElvis(x: String) = x ?: ""
|
|||||||
|
|
||||||
@JavaAnn(1, "abc") class MyClass
|
@JavaAnn(1, "abc") class MyClass
|
||||||
|
|
||||||
|
val i = 1
|
||||||
|
|
||||||
|
annotation class Fancy(val param: Int)
|
||||||
|
|
||||||
|
@Fancy(<caret>i) class D
|
||||||
|
|
||||||
class Foo {
|
class Foo {
|
||||||
var x: Int = 0
|
var x: Int = 0
|
||||||
get = $x
|
get = $x
|
||||||
|
|||||||
@@ -35,6 +35,12 @@ fun unnecessaryElvis(x: String) = x
|
|||||||
|
|
||||||
@JavaAnn(1, arg1 = "abc") class MyClass
|
@JavaAnn(1, arg1 = "abc") class MyClass
|
||||||
|
|
||||||
|
const val i = 1
|
||||||
|
|
||||||
|
annotation class Fancy(val param: Int)
|
||||||
|
|
||||||
|
@Fancy(<caret>i) class D
|
||||||
|
|
||||||
class Foo {
|
class Foo {
|
||||||
var x: Int = 0
|
var x: Int = 0
|
||||||
get = field
|
get = field
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// "Add 'const' modifier" "true"
|
||||||
|
val i = 1
|
||||||
|
|
||||||
|
annotation class Fancy(val param: Int)
|
||||||
|
|
||||||
|
@Fancy(<caret>i) class D
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// "Add 'const' modifier" "true"
|
||||||
|
const val i = 1
|
||||||
|
|
||||||
|
annotation class Fancy(val param: Int)
|
||||||
|
|
||||||
|
@Fancy(<caret>i) class D
|
||||||
@@ -4223,6 +4223,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("constVal.kt")
|
||||||
|
public void testConstVal() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/constVal.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("finalTrait.kt")
|
@TestMetadata("finalTrait.kt")
|
||||||
public void testFinalTrait() throws Exception {
|
public void testFinalTrait() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/finalTrait.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/finalTrait.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user