Redundant lambda arrow: fix false positive in 'when/if' without block #KT-28047 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
0bfcfb5716
commit
4ae837e669
@@ -13,9 +13,13 @@ 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.KtNodeTypes
|
||||
import org.jetbrains.kotlin.psi.KtContainerNodeForControlStructureBody
|
||||
import org.jetbrains.kotlin.psi.KtFunctionLiteral
|
||||
import org.jetbrains.kotlin.psi.KtWhenEntry
|
||||
import org.jetbrains.kotlin.psi.lambdaExpressionVisitor
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
|
||||
|
||||
@@ -28,6 +32,11 @@ class RedundantLambdaArrowInspection : AbstractKotlinInspection() {
|
||||
val singleParameter = parameters.singleOrNull()
|
||||
if (parameters.isNotEmpty() && singleParameter?.isSingleUnderscore != true) return@lambdaExpressionVisitor
|
||||
|
||||
if (lambdaExpression.getStrictParentOfType<KtWhenEntry>()?.expression == lambdaExpression) return@lambdaExpressionVisitor
|
||||
if (lambdaExpression.getStrictParentOfType<KtContainerNodeForControlStructureBody>()?.let {
|
||||
it.node.elementType in listOf(KtNodeTypes.THEN, KtNodeTypes.ELSE) && it.expression == lambdaExpression
|
||||
} == true) return@lambdaExpressionVisitor
|
||||
|
||||
val startOffset = functionLiteral.startOffset
|
||||
holder.registerProblem(
|
||||
holder.manager.createProblemDescriptor(
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// PROBLEM: none
|
||||
fun test(): (Int) -> Int {
|
||||
return if (true) { _ -> 42 } else { <caret>_ -> 42 }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun test(): (Int) -> Int {
|
||||
return if (true) {
|
||||
{ _ -> 42 }
|
||||
} else {
|
||||
{ <caret>_ -> 42 }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun test(): (Int) -> Int {
|
||||
return if (true) {
|
||||
{ _ -> 42 }
|
||||
} else {
|
||||
{ 42 }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// PROBLEM: none
|
||||
fun test(): (Int) -> Int {
|
||||
return if (true) { <caret>_ -> 42 } else { _ -> 42 }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun test(): (Int) -> Int {
|
||||
return if (true) {
|
||||
{ <caret>_ -> 42 }
|
||||
} else {
|
||||
{ _ -> 42 }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun test(): (Int) -> Int {
|
||||
return if (true) {
|
||||
{ 42 }
|
||||
} else {
|
||||
{ _ -> 42 }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// PROBLEM: none
|
||||
fun test(): (Int) -> Int {
|
||||
return when {
|
||||
true -> { <caret>_ -> 42 }
|
||||
else -> { _ -> 42 }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fun test(): (Int) -> Int {
|
||||
return when {
|
||||
true -> {
|
||||
{ <caret>_ -> 42 }
|
||||
}
|
||||
else -> {
|
||||
{ _ -> 42 }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fun test(): (Int) -> Int {
|
||||
return when {
|
||||
true -> {
|
||||
{ 42 }
|
||||
}
|
||||
else -> {
|
||||
{ _ -> 42 }
|
||||
}
|
||||
}
|
||||
}
|
||||
+30
@@ -4233,6 +4233,36 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/hasArguments.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inIfElse.kt")
|
||||
public void testInIfElse() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inIfElse2.kt")
|
||||
public void testInIfElse2() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inIfThen.kt")
|
||||
public void testInIfThen() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inIfThen2.kt")
|
||||
public void testInIfThen2() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inIfWhenEntry.kt")
|
||||
public void testInIfWhenEntry() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inIfWhenEntry2.kt")
|
||||
public void testInIfWhenEntry2() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/simple.kt");
|
||||
|
||||
Reference in New Issue
Block a user