Only when keyword should be highlighted in WhenWithOnlyElseInspection

#KT-31673 Fixed
This commit is contained in:
Dmitry Gridin
2019-05-29 14:23:43 +07:00
parent e955dcfc14
commit e2d5e9a184
18 changed files with 41 additions and 38 deletions
@@ -13,6 +13,7 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.psi.KtWhenExpression import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.whenExpressionVisitor import org.jetbrains.kotlin.psi.whenExpressionVisitor
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
@@ -24,15 +25,17 @@ class WhenWithOnlyElseInspection : AbstractKotlinInspection() {
val usedAsExpression = expression.isUsedAsExpression(expression.analyze()) val usedAsExpression = expression.isUsedAsExpression(expression.analyze())
holder.registerProblem(expression, holder.registerProblem(
"'when' has only 'else' branch and should be simplified", expression,
SimplifyFix(usedAsExpression) expression.whenKeyword.textRange.shiftLeft(expression.startOffset),
"'when' has only 'else' branch and should be simplified",
SimplifyFix(usedAsExpression)
) )
} }
} }
private class SimplifyFix( private class SimplifyFix(
private val isUsedAsExpression: Boolean private val isUsedAsExpression: Boolean
) : LocalQuickFix { ) : LocalQuickFix {
override fun getFamilyName() = name override fun getFamilyName() = name
@@ -1,10 +1,10 @@
fun test() { fun test() {
when (val a = create()) { when<caret> (val a = create()) {
else -> { else -> {
use(a, a) use(a, a)
foo() foo()
} }
}<caret> }
} }
fun create(): String = "" fun create(): String = ""
@@ -1,10 +1,10 @@
fun test() { fun test() {
when (val a = create()) { when<caret> (val a = create()) {
else -> { else -> {
use("") use("")
foo() foo()
} }
}<caret> }
} }
fun create(): String = "" fun create(): String = ""
@@ -1,10 +1,10 @@
fun test() { fun test() {
when (val a = 42) { when<caret> (val a = 42) {
else -> { else -> {
use("") use("")
foo() foo()
} }
}<caret> }
} }
fun use(s: String) {} fun use(s: String) {}
@@ -1,10 +1,10 @@
fun test() { fun test() {
when (val a = create()) { when<caret> (val a = create()) {
else -> { else -> {
use(a) use(a)
foo() foo()
} }
}<caret> }
} }
fun create(): String = "" fun create(): String = ""
@@ -1,10 +1,10 @@
// WITH_RUNTIME // WITH_RUNTIME
fun test() { fun test() {
val x = when (val a = create()) { val x = when<caret> (val a = create()) {
else -> { else -> {
use(a, a) use(a, a)
} }
}<caret> }
} }
fun create(): String = "" fun create(): String = ""
@@ -1,10 +1,10 @@
// WITH_RUNTIME // WITH_RUNTIME
fun test() { fun test() {
val x = when (val a = create()) { val x = when<caret> (val a = create()) {
else -> { else -> {
use("") use("")
} }
}<caret> }
} }
fun create(): String = "" fun create(): String = ""
@@ -1,10 +1,10 @@
// WITH_RUNTIME // WITH_RUNTIME
fun test() { fun test() {
val x = when (val a = 42) { val x = when<caret> (val a = 42) {
else -> { else -> {
use("") use("")
} }
}<caret> }
} }
fun use(s: String) {} fun use(s: String) {}
@@ -1,10 +1,10 @@
// WITH_RUNTIME // WITH_RUNTIME
fun test() { fun test() {
val x = when (val a = create()) { val x = when<caret> (val a = create()) {
else -> { else -> {
use(a) use(a)
} }
}<caret> }
} }
fun create(): String = "" fun create(): String = ""
@@ -1,7 +1,7 @@
fun hasAnnotation() { fun hasAnnotation() {
when (@Bar val a = create()) { when<caret> (@Bar val a = create()) {
else -> use(a) else -> use(a)
}<caret> }
} }
fun create(): String = "" fun create(): String = ""
@@ -1,7 +1,7 @@
fun test() { fun test() {
when (val a = create()) { when<caret> (val a = create()) {
else -> use(a, a) else -> use(a, a)
}<caret> }
} }
fun create(): String = "" fun create(): String = ""
@@ -1,8 +1,8 @@
// WITH_RUNTIME // WITH_RUNTIME
fun test() { fun test() {
val x = when (val a = create()) { val x = when<caret> (val a = create()) {
else -> use(a, a) else -> use(a, a)
}<caret> }
} }
fun create(): String = "" fun create(): String = ""
@@ -1,7 +1,7 @@
fun test() { fun test() {
when (val a = create()) { when<caret> (val a = create()) {
else -> use("") else -> use("")
}<caret> }
} }
fun create(): String = "" fun create(): String = ""
@@ -1,7 +1,7 @@
fun test() { fun test() {
when (val a = 42) { when<caret> (val a = 42) {
else -> use("") else -> use("")
}<caret> }
} }
fun use(s: String) {} fun use(s: String) {}
@@ -1,8 +1,8 @@
// WITH_RUNTIME // WITH_RUNTIME
fun test() { fun test() {
val x = when (val a = create()) { val x = when<caret> (val a = create()) {
else -> use("") else -> use("")
}<caret> }
} }
fun create(): String = "" fun create(): String = ""
@@ -1,8 +1,8 @@
// WITH_RUNTIME // WITH_RUNTIME
fun test() { fun test() {
val x = when (val a = 42) { val x = when<caret> (val a = 42) {
else -> use("") else -> use("")
}<caret> }
} }
fun use(s: String) {} fun use(s: String) {}
@@ -1,7 +1,7 @@
fun test() { fun test() {
when (val a = create()) { when<caret> (val a = create()) {
else -> use(a) else -> use(a)
}<caret> }
} }
fun create(): String = "" fun create(): String = ""
@@ -1,8 +1,8 @@
// WITH_RUNTIME // WITH_RUNTIME
fun test() { fun test() {
val x = when (val a = create()) { val x = when<caret> (val a = create()) {
else -> use(a) else -> use(a)
}<caret> }
} }
fun create(): String = "" fun create(): String = ""