Convert FoldInitializerAndIfToElvisIntention to inspection and decrease severity to INFO

#KT-19643 Fixed
This commit is contained in:
Dmitry Gridin
2019-06-03 13:42:53 +07:00
parent 8b0b135112
commit 74c177fd26
55 changed files with 194 additions and 212 deletions
+1 -6
View File
@@ -865,11 +865,6 @@
<lang.substitutor language="kotlin" order="last" implementationClass="org.jetbrains.kotlin.idea.KotlinLanguageSubstitutor"/>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.FoldInitializerAndIfToElvisIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ImportMemberIntention</className>
<category>Kotlin</category>
@@ -1761,7 +1756,7 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.FoldInitializerAndIfToElvisInspection"
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.FoldInitializerAndIfToElvisInspection"
displayName="If-Null return/break/... foldable to '?:'"
groupPath="Kotlin"
groupName="Style issues"
@@ -1 +0,0 @@
val result = ... <spot>?: return</spot>
@@ -1,2 +0,0 @@
val result = ...
<spot>if (result == null) return</spot>
@@ -1,5 +0,0 @@
<html>
<body>
This intention converts an <b>if</b> expression checking a variable being null or not right after initializing it to an elvis operator in the initializer (if applicable).
</body>
</html>
@@ -1,22 +1,12 @@
/*
* 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.
* Copyright 2010-2019 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.intentions
package org.jetbrains.kotlin.idea.inspections
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
@@ -28,7 +18,6 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.core.setType
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.expressionComparedToNull
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.lexer.KtTokens
@@ -45,18 +34,17 @@ import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
@Suppress("DEPRECATION")
class FoldInitializerAndIfToElvisInspection : IntentionBasedInspection<KtIfExpression>(FoldInitializerAndIfToElvisIntention::class)
class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspection<KtIfExpression>(KtIfExpression::class.java) {
override fun inspectionText(element: KtIfExpression): String = "If-Null return/break/... foldable to '?:'"
class FoldInitializerAndIfToElvisIntention :
SelfTargetingRangeIntention<KtIfExpression>(KtIfExpression::class.java, "Replace 'if' with elvis operator") {
override val defaultFixText: String = "Replace 'if' with elvis operator"
override fun applicabilityRange(element: KtIfExpression): TextRange? {
return FoldInitializerAndIfToElvisIntention.applicabilityRange(element)
}
override fun inspectionRange(element: KtIfExpression): TextRange? = textRange(element)?.shiftLeft(element.startOffset)
override fun applyTo(element: KtIfExpression, editor: Editor?) {
val newElvis = applyTo(element)
override fun isApplicable(element: KtIfExpression): Boolean = Companion.isApplicable(element)
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
val newElvis = Companion.applyTo(element as KtIfExpression)
editor?.caretModel?.moveToOffset(newElvis.right!!.textOffset)
}
@@ -67,8 +55,12 @@ class FoldInitializerAndIfToElvisIntention :
val type = data.ifNullExpression.analyze().getType(data.ifNullExpression) ?: return null
if (!type.isNothing()) return null
val rParen = element.rightParenthesis ?: return null
return TextRange(element.startOffset, rParen.endOffset)
return textRange(element)
}
private fun textRange(element: KtIfExpression): TextRange? {
val rightOffset = element.rightParenthesis?.endOffset ?: return null
return TextRange(element.startOffset, rightOffset)
}
fun isApplicable(element: KtIfExpression): Boolean = applicabilityRange(element) != null
@@ -161,7 +153,12 @@ class FoldInitializerAndIfToElvisIntention :
return null
}
return Data(initializer, prevStatement, statement, typeReference)
return Data(
initializer,
prevStatement,
statement,
typeReference
)
}
private fun PsiChildRange.withoutLastStatement(): PsiChildRange {
@@ -86,7 +86,7 @@ object J2KPostProcessingRegistrarImpl : J2KPostProcessingRegistrar {
_processings.add(UseExpressionBodyProcessing())
registerInspectionBasedProcessing(UnnecessaryVariableInspection())
registerIntentionBasedProcessing(FoldInitializerAndIfToElvisIntention())
registerInspectionBasedProcessing(FoldInitializerAndIfToElvisInspection())
registerIntentionBasedProcessing(FoldIfToReturnIntention()) { it.then.isTrivialStatementBody() && it.`else`.isTrivialStatementBody() }
registerIntentionBasedProcessing(FoldIfToReturnAsymmetricallyIntention()) { it.then.isTrivialStatementBody() && (KtPsiUtil.skipTrailingWhitespacesAndComments(it) as KtReturnExpression).returnedExpression.isTrivialStatementBody() }
@@ -8,7 +8,7 @@ package org.jetbrains.kotlin.idea.joinLines
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate
import com.intellij.openapi.editor.Document
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.idea.intentions.FoldInitializerAndIfToElvisIntention
import org.jetbrains.kotlin.idea.inspections.FoldInitializerAndIfToElvisInspection
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtIfExpression
import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments
@@ -23,8 +23,8 @@ class JoinInitializerAndIfToElvisHandler : JoinRawLinesHandlerDelegate {
?.firstOrNull { it.textContains('\n') }
?: return -1
val ifExpression = lineBreak.getNextSiblingIgnoringWhitespaceAndComments() as? KtIfExpression ?: return -1
if (!FoldInitializerAndIfToElvisIntention.isApplicable(ifExpression)) return -1
return FoldInitializerAndIfToElvisIntention.applyTo(ifExpression).textRange.startOffset
if (!FoldInitializerAndIfToElvisInspection.isApplicable(ifExpression)) return -1
return FoldInitializerAndIfToElvisInspection.applyTo(ifExpression).textRange.startOffset
}
override fun tryJoinLines(document: Document, file: PsiFile, start: Int, end: Int) = -1
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.FoldInitializerAndIfToElvisInspection
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun foo(p: List<String?>): Int {
val v = p[0]
<caret>if (v != null) return -1
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
interface A {
fun a() {}
}
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
open class A
open class B : A() {
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun foo(p: List<String?>) {
val v = p[0]
<caret>if (v == null) {
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun foo(p: List<String?>): Int? {
val v = p[0]
<caret>if (v == null) bar()
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun test(): String {
val foo = foo()
<caret>if (foo !is String?) return "0"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
class C {
var v: String? = null
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
class C {
var x: String? = null
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
interface A {
val s: String
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
interface A {
val s: String
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.FoldInitializerAndIfToElvisIntention
@@ -0,0 +1,8 @@
// "Suppress 'ConstantConditionIf' for fun foo" "true"
fun foo() {
if (<caret>true) {
}
}
// TOOL: org.jetbrains.kotlin.idea.inspections.ConstantConditionIfInspection
@@ -0,0 +1,9 @@
// "Suppress 'ConstantConditionIf' for fun foo" "true"
@Suppress("ConstantConditionIf")
fun foo() {
if (true) {
}
}
// TOOL: org.jetbrains.kotlin.idea.inspections.ConstantConditionIfInspection
@@ -1,9 +0,0 @@
// "Suppress 'FoldInitializerAndIfToElvis' for fun foo" "true"
fun foo(p: List<String?>, b: Boolean) {
var v = p[0]
<caret>if (v == null) return
if (b) v = null
}
// TOOL: org.jetbrains.kotlin.idea.intentions.FoldInitializerAndIfToElvisInspection
@@ -1,10 +0,0 @@
// "Suppress 'FoldInitializerAndIfToElvis' for fun foo" "true"
@Suppress("FoldInitializerAndIfToElvis")
fun foo(p: List<String?>, b: Boolean) {
var v = p[0]
<caret>if (v == null) return
if (b) v = null
}
// TOOL: org.jetbrains.kotlin.idea.intentions.FoldInitializerAndIfToElvisInspection
@@ -3410,6 +3410,139 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class FoldInitializerAndIfToElvis extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInFoldInitializerAndIfToElvis() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("Break.kt")
public void testBreak() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/Break.kt");
}
@TestMetadata("CommentInBlock.kt")
public void testCommentInBlock() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/CommentInBlock.kt");
}
@TestMetadata("Comments.kt")
public void testComments() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/Comments.kt");
}
@TestMetadata("Comments2.kt")
public void testComments2() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/Comments2.kt");
}
@TestMetadata("Continue.kt")
public void testContinue() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/Continue.kt");
}
@TestMetadata("ExplicitValType.kt")
public void testExplicitValType() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/ExplicitValType.kt");
}
@TestMetadata("ExplicitVarType.kt")
public void testExplicitVarType() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/ExplicitVarType.kt");
}
@TestMetadata("IfNotNull.kt")
public void testIfNotNull() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/IfNotNull.kt");
}
@TestMetadata("ifStatementPriority.kt")
public void testIfStatementPriority() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/ifStatementPriority.kt");
}
@TestMetadata("IsSameType.kt")
public void testIsSameType() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/IsSameType.kt");
}
@TestMetadata("IsSideTypeFake.kt")
public void testIsSideTypeFake() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/IsSideTypeFake.kt");
}
@TestMetadata("IsSubType.kt")
public void testIsSubType() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/IsSubType.kt");
}
@TestMetadata("IsSuperTypeFake.kt")
public void testIsSuperTypeFake() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/IsSuperTypeFake.kt");
}
@TestMetadata("MultiStatementBlock.kt")
public void testMultiStatementBlock() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/MultiStatementBlock.kt");
}
@TestMetadata("NotExit.kt")
public void testNotExit() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/NotExit.kt");
}
@TestMetadata("NotIs.kt")
public void testNotIs() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/NotIs.kt");
}
@TestMetadata("NotIsNullableType.kt")
public void testNotIsNullableType() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/NotIsNullableType.kt");
}
@TestMetadata("OtherVar1.kt")
public void testOtherVar1() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/OtherVar1.kt");
}
@TestMetadata("OtherVar2.kt")
public void testOtherVar2() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/OtherVar2.kt");
}
@TestMetadata("Return.kt")
public void testReturn() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/Return.kt");
}
@TestMetadata("ThrowInBlock.kt")
public void testThrowInBlock() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/ThrowInBlock.kt");
}
@TestMetadata("UsedInside.kt")
public void testUsedInside() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/UsedInside.kt");
}
@TestMetadata("UsedInsideInTemplate.kt")
public void testUsedInsideInTemplate() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/UsedInsideInTemplate.kt");
}
@TestMetadata("Var.kt")
public void testVar() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/Var.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/forEachParameterNotUsed")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -8551,139 +8551,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/foldInitializerAndIfToElvis")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class FoldInitializerAndIfToElvis extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInFoldInitializerAndIfToElvis() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/foldInitializerAndIfToElvis"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("Break.kt")
public void testBreak() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/Break.kt");
}
@TestMetadata("CommentInBlock.kt")
public void testCommentInBlock() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/CommentInBlock.kt");
}
@TestMetadata("Comments.kt")
public void testComments() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/Comments.kt");
}
@TestMetadata("Comments2.kt")
public void testComments2() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/Comments2.kt");
}
@TestMetadata("Continue.kt")
public void testContinue() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/Continue.kt");
}
@TestMetadata("ExplicitValType.kt")
public void testExplicitValType() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/ExplicitValType.kt");
}
@TestMetadata("ExplicitVarType.kt")
public void testExplicitVarType() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/ExplicitVarType.kt");
}
@TestMetadata("IfNotNull.kt")
public void testIfNotNull() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/IfNotNull.kt");
}
@TestMetadata("ifStatementPriority.kt")
public void testIfStatementPriority() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/ifStatementPriority.kt");
}
@TestMetadata("IsSameType.kt")
public void testIsSameType() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/IsSameType.kt");
}
@TestMetadata("IsSideTypeFake.kt")
public void testIsSideTypeFake() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/IsSideTypeFake.kt");
}
@TestMetadata("IsSubType.kt")
public void testIsSubType() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/IsSubType.kt");
}
@TestMetadata("IsSuperTypeFake.kt")
public void testIsSuperTypeFake() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/IsSuperTypeFake.kt");
}
@TestMetadata("MultiStatementBlock.kt")
public void testMultiStatementBlock() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/MultiStatementBlock.kt");
}
@TestMetadata("NotExit.kt")
public void testNotExit() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/NotExit.kt");
}
@TestMetadata("NotIs.kt")
public void testNotIs() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/NotIs.kt");
}
@TestMetadata("NotIsNullableType.kt")
public void testNotIsNullableType() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/NotIsNullableType.kt");
}
@TestMetadata("OtherVar1.kt")
public void testOtherVar1() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/OtherVar1.kt");
}
@TestMetadata("OtherVar2.kt")
public void testOtherVar2() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/OtherVar2.kt");
}
@TestMetadata("Return.kt")
public void testReturn() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/Return.kt");
}
@TestMetadata("ThrowInBlock.kt")
public void testThrowInBlock() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/ThrowInBlock.kt");
}
@TestMetadata("UsedInside.kt")
public void testUsedInside() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/UsedInside.kt");
}
@TestMetadata("UsedInsideInTemplate.kt")
public void testUsedInsideInTemplate() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/UsedInsideInTemplate.kt");
}
@TestMetadata("Var.kt")
public void testVar() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/Var.kt");
}
}
@TestMetadata("idea/testData/intentions/implementAbstractMember")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -11879,9 +11879,9 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/suppress/inspections"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("ifNullToElvis.kt")
public void testIfNullToElvis() throws Exception {
runTest("idea/testData/quickfix/suppress/inspections/ifNullToElvis.kt");
@TestMetadata("constantConditionIf.kt")
public void testConstantConditionIf() throws Exception {
runTest("idea/testData/quickfix/suppress/inspections/constantConditionIf.kt");
}
@TestMetadata("unusedImports.kt")
@@ -132,7 +132,7 @@ private val processings: List<GeneralPostProcessing> = listOf(
RemoveExplicitPropertyTypeProcessing(),
RemoveRedundantNullabilityProcessing(),
generalInspectionBasedProcessing(CanBeValInspection(ignoreNotUsedVals = false)),
intentionBasedProcessing(FoldInitializerAndIfToElvisIntention()),
inspectionBasedProcessing(FoldInitializerAndIfToElvisInspection()),
generalInspectionBasedProcessing(RedundantSemicolonInspection()),
intentionBasedProcessing(RemoveEmptyClassBodyIntention()),
intentionBasedProcessing(