Related to KT-20631: don't report redundant blank Unit in lambda

This commit is contained in:
Mikhail Glukhikh
2017-10-31 16:38:55 +03:00
parent 0fd4cdb086
commit d59276671f
7 changed files with 22 additions and 32 deletions
@@ -21,6 +21,7 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
@@ -36,6 +37,9 @@ class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLoc
val parent = expression.parent
if (parent !is KtReturnExpression && parent !is KtBlockExpression) return
// Do not report just 'Unit' in function literals (return@label Unit is OK even in literals)
if (parent is KtBlockExpression && parent.getParentOfType<KtFunctionLiteral>(strict = true) != null) return
holder.registerProblem(expression,
"Redundant 'Unit'",
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
@@ -0,0 +1,12 @@
// PROBLEM: none
fun <T> run(f: () -> T) = f()
fun foo(s: String) = s
fun test() {
run {
foo("Hello")
<caret>Unit
}
}
@@ -1,5 +0,0 @@
fun test() {
val f: () -> Unit = {
<caret>Unit
}
}
@@ -1,4 +0,0 @@
fun test() {
val f: () -> Unit = {
}
}
@@ -1,6 +0,0 @@
fun test() {
val f: () -> Unit = {
<caret>Unit
Unit
}
}
@@ -1,5 +0,0 @@
fun test() {
val f: () -> Unit = {
Unit
}
}
@@ -1637,6 +1637,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantUnitExpression"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("lambda.kt")
public void testLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/lambda.kt");
doTest(fileName);
}
@TestMetadata("notRedundant1.kt")
public void testNotRedundant1() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/notRedundant1.kt");
@@ -1661,18 +1667,6 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
doTest(fileName);
}
@TestMetadata("redundant2.kt")
public void testRedundant2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/redundant2.kt");
doTest(fileName);
}
@TestMetadata("redundant3.kt")
public void testRedundant3() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/redundant3.kt");
doTest(fileName);
}
@TestMetadata("redundant4.kt")
public void testRedundant4() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/redundant4.kt");