add quickfix for NON_CONST_VAL_USED_IN_CONSTANT_EXPRESSION
This commit is contained in:
@@ -16,11 +16,14 @@
|
||||
|
||||
package org.jetbrains.kotlin.resolve
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
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 {
|
||||
override fun check(
|
||||
@@ -31,44 +34,48 @@ public object ConstModifierChecker : DeclarationChecker {
|
||||
) {
|
||||
if (descriptor !is VariableDescriptor || !declaration.hasModifier(JetTokens.CONST_KEYWORD)) return
|
||||
|
||||
val containingDeclaration = descriptor.containingDeclaration
|
||||
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) {
|
||||
diagnosticHolder.report(Errors.WRONG_MODIFIER_TARGET.on(constModifierPsiElement, JetTokens.CONST_KEYWORD, "vars"))
|
||||
return
|
||||
return Errors.WRONG_MODIFIER_TARGET.on(constModifierPsiElement, JetTokens.CONST_KEYWORD, "vars")
|
||||
}
|
||||
|
||||
val containingDeclaration = descriptor.containingDeclaration
|
||||
if (containingDeclaration is ClassDescriptor && containingDeclaration.kind != ClassKind.OBJECT) {
|
||||
diagnosticHolder.report(Errors.CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT.on(constModifierPsiElement))
|
||||
return
|
||||
return Errors.CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT.on(constModifierPsiElement)
|
||||
}
|
||||
|
||||
if (declaration !is JetProperty || descriptor !is PropertyDescriptor) return
|
||||
if (declaration !is JetProperty || descriptor !is PropertyDescriptor) return null
|
||||
|
||||
if (declaration.hasDelegate()) {
|
||||
diagnosticHolder.report(Errors.CONST_VAL_WITH_DELEGATE.on(declaration.delegate!!))
|
||||
return
|
||||
return Errors.CONST_VAL_WITH_DELEGATE.on(declaration.delegate!!)
|
||||
}
|
||||
|
||||
if (descriptor is PropertyDescriptor && !descriptor.getter!!.isDefault) {
|
||||
diagnosticHolder.report(Errors.CONST_VAL_WITH_GETTER.on(declaration.getter!!))
|
||||
return
|
||||
return Errors.CONST_VAL_WITH_GETTER.on(declaration.getter!!)
|
||||
}
|
||||
|
||||
if (!descriptor.type.canBeUsedForConstVal()) {
|
||||
diagnosticHolder.report(Errors.TYPE_CANT_BE_USED_FOR_CONST_VAL.on(constModifierPsiElement, descriptor.type))
|
||||
return
|
||||
return Errors.TYPE_CANT_BE_USED_FOR_CONST_VAL.on(constModifierPsiElement, descriptor.type)
|
||||
}
|
||||
|
||||
if (declaration.initializer == null) {
|
||||
diagnosticHolder.report(Errors.CONST_VAL_WITHOUT_INITIALIZER.on(constModifierPsiElement))
|
||||
return
|
||||
return Errors.CONST_VAL_WITHOUT_INITIALIZER.on(constModifierPsiElement)
|
||||
}
|
||||
|
||||
if (descriptor.compileTimeInitializer == null) {
|
||||
diagnosticHolder.report(Errors.CONST_VAL_WITH_NON_CONST_INITIALIZER.on(declaration.initializer!!))
|
||||
return
|
||||
return Errors.CONST_VAL_WITH_NON_CONST_INITIALIZER.on(declaration.initializer!!)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +90,7 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe
|
||||
ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION,
|
||||
Errors.DEPRECATED_SYMBOL_WITH_MESSAGE,
|
||||
Errors.ACCESS_TO_PRIVATE_TOP_LEVEL_FROM_ANOTHER_FILE,
|
||||
Errors.NON_CONST_VAL_USED_IN_CONSTANT_EXPRESSION,
|
||||
Errors.BACKING_FIELD_SYNTAX_DEPRECATED
|
||||
)
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ import static org.jetbrains.kotlin.lexer.JetTokens.ABSTRACT_KEYWORD;
|
||||
public class AddModifierFix extends JetIntentionAction<JetModifierListOwner> {
|
||||
@NotNull private final JetModifierKeywordToken modifier;
|
||||
|
||||
private AddModifierFix(@NotNull JetModifierListOwner element, @NotNull JetModifierKeywordToken modifier) {
|
||||
public AddModifierFix(@NotNull JetModifierListOwner element, @NotNull JetModifierKeywordToken modifier) {
|
||||
super(element);
|
||||
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)
|
||||
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_USAGE_DEPRECATED.registerFactory(MigrateBackingFieldUsageFix)
|
||||
BACKING_FIELD_USAGE_DEPRECATED.registerFactory(IntroduceBackingPropertyFix)
|
||||
|
||||
@@ -36,6 +36,12 @@ fun unnecessaryElvis(x: String) = x ?: ""
|
||||
|
||||
@JavaAnn(1, "abc") class MyClass
|
||||
|
||||
val i = 1
|
||||
|
||||
annotation class Fancy(val param: Int)
|
||||
|
||||
@Fancy(<caret>i) class D
|
||||
|
||||
class Foo {
|
||||
var x: Int = 0
|
||||
get = $x
|
||||
|
||||
@@ -35,6 +35,12 @@ fun unnecessaryElvis(x: String) = x
|
||||
|
||||
@JavaAnn(1, arg1 = "abc") class MyClass
|
||||
|
||||
const val i = 1
|
||||
|
||||
annotation class Fancy(val param: Int)
|
||||
|
||||
@Fancy(<caret>i) class D
|
||||
|
||||
class Foo {
|
||||
var x: Int = 0
|
||||
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);
|
||||
}
|
||||
|
||||
@TestMetadata("constVal.kt")
|
||||
public void testConstVal() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/constVal.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("finalTrait.kt")
|
||||
public void testFinalTrait() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/finalTrait.kt");
|
||||
|
||||
Reference in New Issue
Block a user