KT-12019 Highlighting of redundant 'if' statements (#871)

This commit is contained in:
Vladislav Golub
2016-05-23 21:02:38 +03:00
committed by Dmitry Jemerov
parent cf56ac0305
commit bb32c2d350
12 changed files with 180 additions and 2 deletions

View File

@@ -333,8 +333,15 @@ public class KtPsiUtil {
}
public static boolean isTrueConstant(@Nullable KtExpression condition) {
return (condition != null && condition.getNode().getElementType() == KtNodeTypes.BOOLEAN_CONSTANT &&
condition.getNode().findChildByType(KtTokens.TRUE_KEYWORD) != null);
return isBooleanConstant(condition) && condition.getNode().findChildByType(KtTokens.TRUE_KEYWORD) != null;
}
public static boolean isFalseConstant(@Nullable KtExpression condition) {
return isBooleanConstant(condition) && condition.getNode().findChildByType(KtTokens.FALSE_KEYWORD) != null;
}
private static boolean isBooleanConstant(@Nullable KtExpression condition) {
return condition != null && condition.getNode().getElementType() == KtNodeTypes.BOOLEAN_CONSTANT;
}
public static boolean isAbstract(@NotNull KtDeclarationWithBody declaration) {

View File

@@ -0,0 +1,21 @@
<html>
<body>
Reports <b>if</b> statements which can be simplified to single <b>return</b> statements.
<p>
For example:
<code><pre>
<b>if</b> (foo()) {
<b>return true</b>
} <b>else</b> {
<b>return false</b>
}
</pre></code>
can be simplified to
<code><pre>
<b>return</b> foo()
</pre></code>
<!-- tooltip end -->
<p>
</body>
</html>

View File

@@ -1532,6 +1532,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RedundantIfInspection"
displayName="Redundant 'if' statement"
groupName="Kotlin"
enabledByDefault="true"
cleanupTool="true"
level="WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2010-2016 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.psi.*
class RedundantIfInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
return object : KtVisitorVoid() {
override fun visitIfExpression(expression: KtIfExpression) {
super.visitIfExpression(expression)
if (expression.condition != null && isRedundant(expression)) {
holder.registerProblem(expression,
"Redundant 'if' statement",
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
RemoveRedundantIf)
}
}
}
}
private fun isRedundant(expression: KtIfExpression): Boolean {
val thenReturn = getReturnedExpression(expression.then) ?: return false
val elseReturn = getReturnedExpression(expression.`else`) ?: return false
if (KtPsiUtil.isTrueConstant(thenReturn) && KtPsiUtil.isFalseConstant(elseReturn)) {
return true
}
if (KtPsiUtil.isFalseConstant(thenReturn) && KtPsiUtil.isTrueConstant(elseReturn)) {
return true
}
return false
}
private fun getReturnedExpression(expression: KtExpression?) : KtExpression? {
when(expression) {
is KtReturnExpression -> return expression.returnedExpression
is KtBlockExpression -> {
val statement = expression.statements.singleOrNull() as? KtReturnExpression ?: return null
return statement.returnedExpression
}
else -> return null
}
}
private object RemoveRedundantIf : LocalQuickFix {
override fun getName() = "Remove redundant 'if' statement"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.psiElement as KtIfExpression
element.replace(KtPsiFactory(element).createExpressionByPattern("return $0", element.condition!!.text));
}
}
}

View File

@@ -0,0 +1,18 @@
<problems>
<problem>
<file>redundantIf.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="redundantIf.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant 'if' statement</problem_class>
<description>Redundant 'if' statement</description>
</problem>
<problem>
<file>redundantIf.kt</file>
<line>10</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="redundantIf.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant 'if' statement</problem_class>
<description>Redundant 'if' statement</description>
</problem>
</problems>

View File

@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.RedundantIfInspection

View File

@@ -0,0 +1,11 @@
fun foo() {
if (value % 2 == 0) {
return true
} else {
return false
}
}
fun bar() {
if (value % 2 == 0) return true else return false
}

View File

@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.RedundantIfInspection

View File

@@ -0,0 +1,8 @@
// "Remove redundant 'if' statement" "true"
fun bar() {
<caret>if (value % 2 == 0) {
return true
} else {
return false
}
}

View File

@@ -0,0 +1,4 @@
// "Remove redundant 'if' statement" "true"
fun bar() {
return value % 2 == 0
}

View File

@@ -160,6 +160,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
doTest(fileName);
}
@TestMetadata("redundantIf/inspectionData/inspections.test")
public void testRedundantIf_inspectionData_Inspections_test() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/redundantIf/inspectionData/inspections.test");
doTest(fileName);
}
@TestMetadata("redundantModalityModifier/inspectionData/inspections.test")
public void testRedundantModalityModifier_inspectionData_Inspections_test() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/redundantModalityModifier/inspectionData/inspections.test");

View File

@@ -6383,6 +6383,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/redundantIf")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RedundantIf extends AbstractQuickFixTest {
public void testAllFilesPresentInRedundantIf() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/redundantIf"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/redundantIf/simple.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/redundantModalityModifier")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)