Skip to content

Commit

Permalink
Merge #4805
Browse files Browse the repository at this point in the history
4805: INT: do not suggest "Lift return out" on 'if' in 'else' branch r=Undin a=t-kameyama

Fixes #4789

Co-authored-by: Toshiaki Kameyama <kmymtsak@gmail.com>
  • Loading branch information
bors[bot] and t-kameyama committed Dec 31, 2019
2 parents a50a9a0 + 1a59d11 commit 4613262
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main/kotlin/org/rust/ide/inspections/RsLiftInspection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class RsLiftInspection : RsLocalInspectionTool() {
override fun buildVisitor(holder: RsProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor? {
return object : RsVisitor() {
override fun visitIfExpr(o: RsIfExpr) {
if (o.parent is RsElseBranch) return
checkExpr(o, o.`if`)
}

Expand Down Expand Up @@ -91,7 +92,12 @@ private fun RsExpr.getFoldableReturns(): List<FoldableElement>? {
}
is RsIfExpr -> {
if (block?.collectFoldableReturns() != true) return false
if (elseBranch?.block?.collectFoldableReturns() != true) return false
val elseIf = elseBranch?.ifExpr
if (elseIf != null) {
if (!elseIf.collectFoldableReturns()) return false
} else {
if (elseBranch?.block?.collectFoldableReturns() != true) return false
}
}
is RsMatchExpr -> {
val arms = matchBody?.matchArmList ?: return false
Expand Down
38 changes: 38 additions & 0 deletions src/test/kotlin/org/rust/ide/inspections/RsLiftInspectionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,44 @@ class RsLiftInspectionTest : RsInspectionsTestBase(RsLiftInspection::class) {
}
""", checkWeakWarn = true, testmark = RsLiftInspection.Testmarks.insideRetExpr)

fun `test lift return in else if`() = checkFixIsUnavailable("Lift return out of 'if'", """
fn get_char(n: u32) -> char {
/*weak_warning descr="Return can be lifted out of 'if'"*/if/*weak_warning**/ n == 0 {
return 'a'
} else /*caret*/if n == 1 {
return 'b'
} else {
return 'c'
}
}
""", checkWeakWarn = true)

fun `test lift return in if with else if`() = checkFixByText("Lift return out of 'if'", """
fn get_char(n: u32) -> char {
/*weak_warning descr="Return can be lifted out of 'if'"*/if/*caret*//*weak_warning**/ n == 0 {
return 'a'
} else if n == 1 {
return 'b'
} else if n == 2 {
return 'c'
} else {
return 'd'
}
}
""", """
fn get_char(n: u32) -> char {
return if n == 0 {
'a'
} else if n == 1 {
'b'
} else if n == 2 {
'c'
} else {
'd'
}
}
""", checkWeakWarn = true)

fun `test lift return in match 1`() = checkFixByText("Lift return out of 'match'", """
fn foo(x: bool) -> i32 {
/*weak_warning descr="Return can be lifted out of 'match'"*/match/*caret*//*weak_warning**/ x {
Expand Down

0 comments on commit 4613262

Please sign in to comment.