"Lift return / assignment": do not suggest for else arguments

This commit is contained in:
Mikhail Glukhikh
2017-07-06 15:13:03 +03:00
parent 042990dda1
commit 691b733c55
4 changed files with 32 additions and 0 deletions
@@ -20,6 +20,7 @@ import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isIfBranch
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.lineCount
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtIfExpression
@@ -33,6 +34,8 @@ class LiftReturnOrAssignmentInspection : AbstractKotlinInspection() {
object : KtVisitorVoid() {
private fun visitIfOrWhen(expression: KtExpression, keyword: PsiElement) {
if (expression.lineCount() > LINES_LIMIT) return
if (expression.isIfBranch()) return
val returnNumber = BranchedFoldingUtils.getFoldableReturnNumber(expression)
if (returnNumber > 0) {
holder.registerProblem(
@@ -264,3 +264,11 @@ internal fun KtExpression.lineCount(): Int {
}
internal fun KtExpression.isOneLiner(): Boolean = lineCount() == 1
internal fun KtExpression.isIfBranch(): Boolean {
if (parent is KtContainerNodeForControlStructureBody) {
val grandParent = parent.parent
if (grandParent is KtIfExpression && grandParent.`else` == this) return true
}
return false
}
@@ -0,0 +1,15 @@
// PROBLEM: none
fun foo(a: Boolean, b: Boolean): String {
var res = ""
if (a) {
}
else <caret>if (b) {
res += "b"
}
else {
res += "!b"
}
return res
}
@@ -418,6 +418,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
doTest(fileName);
}
@TestMetadata("elseIfOnly.kt")
public void testElseIfOnly() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/liftOut/ifToAssignment/elseIfOnly.kt");
doTest(fileName);
}
@TestMetadata("ifElseIf.kt")
public void testIfElseIf() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/liftOut/ifToAssignment/ifElseIf.kt");