Introduce inspection to detect use of Unit as a standalone expression

So #KT-20631 Fixed
This commit is contained in:
Toshiaki Kameyama
2017-10-12 10:28:21 +03:00
committed by Mikhail Glukhikh
parent 5f4233e4bf
commit ef71f7707d
18 changed files with 196 additions and 0 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports redundant 'Unit' which can be omitted.
</body>
</html>
+9
View File
@@ -2624,6 +2624,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RedundantUnitExpressionInspection"
displayName="Redundant 'Unit'"
groupPath="Kotlin"
groupName="Redundant constructs"
enabledByDefault="true"
level="WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -0,0 +1,56 @@
/*
* 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.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.psi.*
class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
return object : KtVisitorVoid() {
override fun visitReferenceExpression(expression: KtReferenceExpression) {
super.visitReferenceExpression(expression)
if (KotlinBuiltIns.FQ_NAMES.unit.shortName() != (expression as? KtNameReferenceExpression)?.getReferencedNameAsName()) {
return
}
val parent = expression.parent
if (parent !is KtReturnExpression && parent !is KtBlockExpression) return
holder.registerProblem(expression,
"Redundant 'Unit'",
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
RemoveRedundantUnitFix())
}
}
}
}
private class RemoveRedundantUnitFix : LocalQuickFix {
override fun getName() = "Remove redundant 'Unit'"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
(descriptor.psiElement as? KtReferenceExpression)?.delete()
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.RedundantUnitExpressionInspection
@@ -0,0 +1,4 @@
// PROBLEM: none
fun test() {
val u = <caret>Unit
}
@@ -0,0 +1,2 @@
// PROBLEM: none
fun test() = <caret>Unit
@@ -0,0 +1,7 @@
// PROBLEM: none
fun test(s: String?) {
val x: Any = if (s == null)
""
else
Unit<caret>
}
@@ -0,0 +1,3 @@
fun test() {
return <caret>Unit
}
@@ -0,0 +1,3 @@
fun test() {
return
}
@@ -0,0 +1,5 @@
fun test() {
val f: () -> Unit = {
<caret>Unit
}
}
@@ -0,0 +1,4 @@
fun test() {
val f: () -> Unit = {
}
}
@@ -0,0 +1,6 @@
fun test() {
val f: () -> Unit = {
<caret>Unit
Unit
}
}
@@ -0,0 +1,5 @@
fun test() {
val f: () -> Unit = {
Unit
}
}
@@ -0,0 +1,8 @@
fun test(s: String?) {
val x: Any = if (s == null) {
""
}
else {
<caret>Unit
}
}
@@ -0,0 +1,7 @@
fun test(s: String?) {
val x: Any = if (s == null) {
""
}
else {
}
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun test() {
val x: List<Unit> = listOf(1, 2, 3).map {
return@map <caret>Unit
}
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun test() {
val x: List<Unit> = listOf(1, 2, 3).map {
return@map
}
}
@@ -1623,6 +1623,63 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/redundantUnitExpression")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RedundantUnitExpression extends AbstractLocalInspectionTest {
public void testAllFilesPresentInRedundantUnitExpression() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantUnitExpression"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("notRedundant1.kt")
public void testNotRedundant1() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/notRedundant1.kt");
doTest(fileName);
}
@TestMetadata("notRedundant2.kt")
public void testNotRedundant2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/notRedundant2.kt");
doTest(fileName);
}
@TestMetadata("notRedundant3.kt")
public void testNotRedundant3() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/notRedundant3.kt");
doTest(fileName);
}
@TestMetadata("redundant1.kt")
public void testRedundant1() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/redundant1.kt");
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");
doTest(fileName);
}
@TestMetadata("redundant5.kt")
public void testRedundant5() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/redundant5.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)