Revert back Inspection: convert initialized val to non-null type

Relates to ^KT-35757
Relates to ^KT-19321
This commit is contained in:
Vladimir Dolzhenko
2020-09-09 23:36:41 +02:00
parent 12ebd429bc
commit 6f03e42ef7
13 changed files with 1 additions and 152 deletions
@@ -1,5 +0,0 @@
<html>
<body>
This inspection reports nullable type of initialized 'val' should be converted into non-nullable type.
</body>
</html>
@@ -1359,8 +1359,6 @@ condition.is.always.0=Condition is always ''{0}''
remove.fix.text=Delete expression
simplify.fix.text=Simplify expression
0.has.empty.body=''{0}'' has empty body
initialized.val.should.be.converted.to.non.null.type=Initialized ''val'' should be converted to non-null type
convert.initialized.val.to.non.null.type.quick.fix.text=Convert initialized ''val'' to non-null type
convert.na.n.equality.quick.fix.text=Replace with 'isNaN()'
equality.check.with.nan.should.be.replaced.with.isnan=Equality check with NaN should be replaced with 'isNaN()'
convert.pair.constructor.to.to.fix.text=Convert to 'to'
@@ -2001,7 +1999,7 @@ action.ConfigureKotlinJsInProject.text=Configure Kotlin (JavaScript) in Project
action.ConfigureKotlinInProject.text=Configure Kotlin in Project
action.KotlinConsoleREPL.text=Kotlin REPL
action.LibraryToSourceDependencySupportToggleAction.text=Toggle library to source dependency support
inspection.convert.initialized.val.to.non.null.type.display.name=Convert initialized 'val' to non-null type
inspection.unused.unary.operator.display.name=Unused unary operator
inspection.incomplete.destructuring.declaration.display.name=Incomplete destructuring declaration
inspection.replace.guard.clause.with.function.call.display.name=Replace guard clause with kotlin's function call
-8
View File
@@ -2503,14 +2503,6 @@
language="kotlin"
key="inspection.unused.unary.operator.display.name" bundle="messages.KotlinBundle"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ConvertInitializedValToNonNullTypeInspection"
groupPath="Kotlin"
groupName="Probable bugs"
enabledByDefault="true"
level="INFO"
language="kotlin"
key="inspection.convert.initialized.val.to.non.null.type.display.name" bundle="messages.KotlinBundle"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.IncompleteDestructuringInspection"
groupPath="Kotlin"
groupName="Probable bugs"
@@ -1,71 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.isNull
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.TypeUtils
class ConvertInitializedValToNonNullTypeInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) =
propertyVisitor(fun(property) {
val typeReference = property.typeReference ?: return
if (shouldConvertToNonNullType(property)) {
holder.registerProblem(
typeReference,
KotlinBundle.message("initialized.val.should.be.converted.to.non.null.type"),
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
RemoveRedundantNullableTypeQuickfix()
)
}
})
companion object {
private fun shouldConvertToNonNullType(property: KtProperty): Boolean {
val initializer = property.initializer ?: return false
val type = property.resolveToDescriptorIfAny()?.type ?: return false
if (!TypeUtils.isNullableType(type)) return false
when (initializer) {
is KtConstantExpression -> {
return !initializer.isNull()
}
is KtStringTemplateExpression -> {
return true
}
is KtNameReferenceExpression, is KtCallExpression -> {
val context = initializer.analyze(BodyResolveMode.PARTIAL)
val assignedType = initializer.getType(context) ?: return false
return !TypeUtils.isNullableType(assignedType)
}
else -> return false
}
}
}
}
class RemoveRedundantNullableTypeQuickfix : LocalQuickFix {
override fun getName() = KotlinBundle.message("convert.initialized.val.to.non.null.type.quick.fix.text")
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.psiElement
val factory = KtPsiFactory(project)
element.replace(factory.createType(element.text.removeSuffix("?")))
}
}
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.inspections.ConvertInitializedValToNonNullTypeInspection
@@ -1,5 +0,0 @@
fun foo() {
val s1: String?<caret> = bar()
}
fun bar(): String = ""
@@ -1,5 +0,0 @@
fun foo() {
val s1: String = bar()
}
fun bar(): String = ""
@@ -1,4 +0,0 @@
fun foo() {
val s0: String = ""
val s1: String?<caret> = s0
}
@@ -1,4 +0,0 @@
fun foo() {
val s0: String = ""
val s1: String = s0
}
@@ -1,3 +0,0 @@
fun foo() {
val s: String?<caret> = "Hello"
}
@@ -1,3 +0,0 @@
fun foo() {
val s: String = "Hello"
}
@@ -1,7 +0,0 @@
// PROBLEM: none
fun foo() {
val s1: String? = bar()
val s2: String?<caret> = null
}
fun bar(): String? = ""
@@ -3485,39 +3485,6 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/convertInitializedValToNonNullType")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertInitializedValToNonNullType extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInConvertInitializedValToNonNullType() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/inspectionsLocal/convertInitializedValToNonNullType"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true);
}
@TestMetadata("assignToMethodCall.kt")
public void testAssignToMethodCall() throws Exception {
runTest("idea/testData/inspectionsLocal/convertInitializedValToNonNullType/assignToMethodCall.kt");
}
@TestMetadata("assignToVariable.kt")
public void testAssignToVariable() throws Exception {
runTest("idea/testData/inspectionsLocal/convertInitializedValToNonNullType/assignToVariable.kt");
}
@TestMetadata("basic.kt")
public void testBasic() throws Exception {
runTest("idea/testData/inspectionsLocal/convertInitializedValToNonNullType/basic.kt");
}
@TestMetadata("negative.kt")
public void testNegative() throws Exception {
runTest("idea/testData/inspectionsLocal/convertInitializedValToNonNullType/negative.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/convertNaNEquality")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)