Introduce "Redundant '?: return null'" inspection

#KT-34819 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-11-10 13:12:22 +01:00
committed by Vladimir Dolzhenko
parent 5f1cc3b152
commit 820b8c3c54
12 changed files with 162 additions and 0 deletions
@@ -3577,6 +3577,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RedundantElvisReturnNullInspection"
displayName="Redundant '?: return null'"
groupPath="Kotlin"
groupName="Redundant constructs"
enabledByDefault="true"
level="WARNING"
language="kotlin"
/>
<projectService serviceInterface="org.jetbrains.kotlin.idea.codeInsight.KotlinCodeInsightWorkspaceSettings"
serviceImplementation="org.jetbrains.kotlin.idea.codeInsight.KotlinCodeInsightWorkspaceSettings"/>
@@ -3578,6 +3578,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RedundantElvisReturnNullInspection"
displayName="Redundant '?: return null'"
groupPath="Kotlin"
groupName="Redundant constructs"
enabledByDefault="true"
level="WARNING"
language="kotlin"
/>
<projectService serviceInterface="org.jetbrains.kotlin.idea.codeInsight.KotlinCodeInsightWorkspaceSettings"
serviceImplementation="org.jetbrains.kotlin.idea.codeInsight.KotlinCodeInsightWorkspaceSettings"/>
@@ -3578,6 +3578,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RedundantElvisReturnNullInspection"
displayName="Redundant '?: return null'"
groupPath="Kotlin"
groupName="Redundant constructs"
enabledByDefault="true"
level="WARNING"
language="kotlin"
/>
<projectService serviceInterface="org.jetbrains.kotlin.idea.codeInsight.KotlinCodeInsightWorkspaceSettings"
serviceImplementation="org.jetbrains.kotlin.idea.codeInsight.KotlinCodeInsightWorkspaceSettings"/>
@@ -0,0 +1,15 @@
<html>
<body>
This inspection reports redundant '<b>?: return null</b>':
<br /><br />
<pre>
fun foo(): Int? {
...
}
fun test() : Int? {
return foo() ?: return null
}
</pre>
</body>
</html>
@@ -0,0 +1,57 @@
/*
* 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.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 com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.startOffset
class RedundantElvisReturnNullInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return returnExpressionVisitor(fun(returnExpression: KtReturnExpression) {
if ((returnExpression.returnedExpression?.deparenthesize() as? KtConstantExpression)?.text != KtTokens.NULL_KEYWORD.value) return
val binaryExpression = returnExpression.getStrictParentOfType<KtBinaryExpression>()?.takeIf {
it == it.getStrictParentOfType<KtReturnExpression>()?.returnedExpression?.deparenthesize()
} ?: return
val right = binaryExpression.right?.deparenthesize()?.takeIf { it == returnExpression } ?: return
if (binaryExpression.operationToken == KtTokens.ELSE_KEYWORD) return
if (binaryExpression.left?.resolveToCall()?.resultingDescriptor?.returnType?.isMarkedNullable != true) return
holder.registerProblem(
binaryExpression,
"Redundant '?: return null'",
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
TextRange(binaryExpression.operationReference.startOffset, right.endOffset).shiftLeft(binaryExpression.startOffset),
RemoveRedundantElvisReturnNull()
)
})
}
private fun KtExpression.deparenthesize() = KtPsiUtil.deparenthesize(this)
private class RemoveRedundantElvisReturnNull : LocalQuickFix {
override fun getName() = "Remove redundant '?: return null'"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val binaryExpression = descriptor.psiElement as? KtBinaryExpression ?: return
val left = binaryExpression.left ?: return
binaryExpression.replace(left)
}
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.RedundantElvisReturnNullInspection
@@ -0,0 +1,5 @@
fun foo(): Int? = null
fun test() : Int? {
return foo() <caret>?: return null
}
@@ -0,0 +1,5 @@
fun foo(): Int? = null
fun test() : Int? {
return foo()
}
@@ -0,0 +1,7 @@
// PROBLEM: none
fun foo(): Int? = null
fun test() : Int? {
val i = foo() <caret>?: return null
return 0
}
@@ -0,0 +1,6 @@
// PROBLEM: none
fun foo(): Int? = null
fun test() : Int? {
return foo() <caret>?: return 1
}
@@ -0,0 +1,6 @@
// PROBLEM: none
fun bar(): Int = 1
fun test() : Int? {
return bar() <caret>?: return null // USELESS_ELVIS
}
@@ -7144,6 +7144,39 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/redundantElvisReturnNull")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RedundantElvisReturnNull extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInRedundantElvisReturnNull() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantElvisReturnNull"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), true);
}
@TestMetadata("basic.kt")
public void testBasic() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantElvisReturnNull/basic.kt");
}
@TestMetadata("notInReturn.kt")
public void testNotInReturn() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantElvisReturnNull/notInReturn.kt");
}
@TestMetadata("notReturnNull.kt")
public void testNotReturnNull() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantElvisReturnNull/notReturnNull.kt");
}
@TestMetadata("uselessElvis.kt")
public void testUselessElvis() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantElvisReturnNull/uselessElvis.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/redundantEmptyInitializerBlock")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)