Add inspection for calls of function with lambda expression body
Added "Unused return value of a function with lambda expression body" inspection with quickfix "Remove '=' token from function declaration" #KT-10393 Fixed
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports calls with unused return value when the function is defined with lambda as expression body.
|
||||
Happens when someone accidentally puts '=' between function header and body block.
|
||||
</body>
|
||||
</html>
|
||||
@@ -2071,6 +2071,14 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnusedLambdaExpressionBodyInspection"
|
||||
displayName="Unused return value of a function with lambda expression body"
|
||||
groupName="Kotlin"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.inspections
|
||||
|
||||
import com.intellij.codeInsight.FileModificationService
|
||||
import com.intellij.codeInspection.LocalQuickFix
|
||||
import com.intellij.codeInspection.ProblemDescriptor
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.builtins.isFunctionType
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.source.getPsi
|
||||
|
||||
|
||||
class UnusedLambdaExpressionBodyInspection : AbstractKotlinInspection() {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return object : KtVisitorVoid() {
|
||||
override fun visitCallExpression(expression: KtCallExpression) {
|
||||
val context = expression.analyze(BodyResolveMode.PARTIAL)
|
||||
if (expression.used(context)) {
|
||||
return
|
||||
}
|
||||
|
||||
val descriptor = expression.getResolvedCall(context)?.resultingDescriptor ?: return
|
||||
if (!descriptor.returnsFunction()) {
|
||||
return
|
||||
}
|
||||
|
||||
val function = descriptor.source.getPsi() as? KtFunction ?: return
|
||||
if (function.hasBlockBody() || function.bodyExpression !is KtLambdaExpression) {
|
||||
return
|
||||
}
|
||||
|
||||
holder.registerProblem(expression,
|
||||
"Unused return value of a function with lambda expression body",
|
||||
RemoveEqTokenFromFunctionDeclarationFix(function))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtExpression.used(context: BindingContext): Boolean = context[BindingContext.USED_AS_EXPRESSION, this] ?: true
|
||||
|
||||
private fun CallableDescriptor.returnsFunction() = returnType?.isFunctionType ?: false
|
||||
|
||||
class RemoveEqTokenFromFunctionDeclarationFix(val function: KtFunction) : LocalQuickFix {
|
||||
override fun getName(): String = "Remove '=' token from function declaration"
|
||||
|
||||
override fun getFamilyName(): String = name
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
if (!FileModificationService.getInstance().preparePsiElementForWrite(function)) {
|
||||
return
|
||||
}
|
||||
|
||||
function.equalsToken?.apply {
|
||||
// TODO: This should be done by formatter but there is no rule for this now
|
||||
if (prevSibling.isSpace() && nextSibling.isSpace()) {
|
||||
prevSibling.delete()
|
||||
}
|
||||
delete()
|
||||
}
|
||||
}
|
||||
|
||||
private fun PsiElement.isSpace() = text == " "
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>simple.kt</file>
|
||||
<line>3</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/simple.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused return value of a function with lambda expression body</problem_class>
|
||||
<description>Unused return value of a function with lambda expression body</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>simple.kt</file>
|
||||
<line>6</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/simple.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused return value of a function with lambda expression body</problem_class>
|
||||
<description>Unused return value of a function with lambda expression body</description>
|
||||
</problem>
|
||||
</problems>
|
||||
+1
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.UnusedLambdaExpressionBodyInspection
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
fun foo() {
|
||||
bar()
|
||||
val a = bar()
|
||||
if (bar() != null) {
|
||||
bar()
|
||||
}
|
||||
bar()()
|
||||
}
|
||||
|
||||
fun bar() = {
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.UnusedLambdaExpressionBodyInspection
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Remove '=' token from function declaration" "true"
|
||||
|
||||
fun foo() {
|
||||
<caret>bar()
|
||||
}
|
||||
|
||||
fun bar() = {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Remove '=' token from function declaration" "true"
|
||||
|
||||
fun foo() {
|
||||
bar()
|
||||
}
|
||||
|
||||
fun bar() {
|
||||
|
||||
}
|
||||
@@ -311,6 +311,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("unusedLambdaExpressionBody/inspectionData/inspections.test")
|
||||
public void testUnusedLambdaExpressionBody_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/unusedLambdaExpressionBody/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("unusedReceiverParameter/inspectionData/inspections.test")
|
||||
public void testUnusedReceiverParameter_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/unusedReceiverParameter/inspectionData/inspections.test");
|
||||
|
||||
@@ -7732,6 +7732,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/removeEqTokenFromFunctionDeclaration")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RemoveEqTokenFromFunctionDeclaration extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInRemoveEqTokenFromFunctionDeclaration() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeEqTokenFromFunctionDeclaration"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeEqTokenFromFunctionDeclaration/simple.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/removeFinalUpperBound")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user