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 org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.whenExpressionVisitor
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
@@ -24,15 +25,17 @@ class WhenWithOnlyElseInspection : AbstractKotlinInspection() {
val usedAsExpression = expression.isUsedAsExpression(expression.analyze())
holder.registerProblem(expression,
"'when' has only 'else' branch and should be simplified",
SimplifyFix(usedAsExpression)
holder.registerProblem(
expression,
expression.whenKeyword.textRange.shiftLeft(expression.startOffset),
"'when' has only 'else' branch and should be simplified",
SimplifyFix(usedAsExpression)
)
}
}
private class SimplifyFix(
private val isUsedAsExpression: Boolean
private val isUsedAsExpression: Boolean
) : LocalQuickFix {
override fun getFamilyName() = name
@@ -1,10 +1,10 @@
fun test() {
when (val a = create()) {
when<caret> (val a = create()) {
else -> {
use(a, a)
foo()
}
}<caret>
}
}
fun create(): String = ""
@@ -1,10 +1,10 @@
fun test() {
when (val a = create()) {
when<caret> (val a = create()) {
else -> {
use("")
foo()
}
}<caret>
}
}
fun create(): String = ""
@@ -1,10 +1,10 @@
fun test() {
when (val a = 42) {
when<caret> (val a = 42) {
else -> {
use("")
foo()
}
}<caret>
}
}
fun use(s: String) {}
@@ -1,10 +1,10 @@
fun test() {
when (val a = create()) {
when<caret> (val a = create()) {
else -> {
use(a)
foo()
}
}<caret>
}
}
fun create(): String = ""
@@ -1,10 +1,10 @@
// WITH_RUNTIME
fun test() {
val x = when (val a = create()) {
val x = when<caret> (val a = create()) {
else -> {
use(a, a)
}
}<caret>
}
}
fun create(): String = ""
@@ -1,10 +1,10 @@
// WITH_RUNTIME
fun test() {
val x = when (val a = create()) {
val x = when<caret> (val a = create()) {
else -> {
use("")
}
}<caret>
}
}
fun create(): String = ""
@@ -1,10 +1,10 @@
// WITH_RUNTIME
fun test() {
val x = when (val a = 42) {
val x = when<caret> (val a = 42) {
else -> {
use("")
}
}<caret>
}
}
fun use(s: String) {}
@@ -1,10 +1,10 @@
// WITH_RUNTIME
fun test() {
val x = when (val a = create()) {
val x = when<caret> (val a = create()) {
else -> {
use(a)
}
}<caret>
}
}
fun create(): String = ""
@@ -1,7 +1,7 @@
fun hasAnnotation() {
when (@Bar val a = create()) {
when<caret> (@Bar val a = create()) {
else -> use(a)
}<caret>
}
}
fun create(): String = ""
@@ -1,7 +1,7 @@
fun test() {
when (val a = create()) {
when<caret> (val a = create()) {
else -> use(a, a)
}<caret>
}
}
fun create(): String = ""
@@ -1,8 +1,8 @@
// WITH_RUNTIME
fun test() {
val x = when (val a = create()) {
val x = when<caret> (val a = create()) {
else -> use(a, a)
}<caret>
}
}
fun create(): String = ""
@@ -1,7 +1,7 @@
fun test() {
when (val a = create()) {
when<caret> (val a = create()) {
else -> use("")
}<caret>
}
}
fun create(): String = ""
@@ -1,7 +1,7 @@
fun test() {
when (val a = 42) {
when<caret> (val a = 42) {
else -> use("")
}<caret>
}
}
fun use(s: String) {}
@@ -1,8 +1,8 @@
// WITH_RUNTIME
fun test() {
val x = when (val a = create()) {
val x = when<caret> (val a = create()) {
else -> use("")
}<caret>
}
}
fun create(): String = ""
@@ -1,8 +1,8 @@
// WITH_RUNTIME
fun test() {
val x = when (val a = 42) {
val x = when<caret> (val a = 42) {
else -> use("")
}<caret>
}
}
fun use(s: String) {}
@@ -1,7 +1,7 @@
fun test() {
when (val a = create()) {
when<caret> (val a = create()) {
else -> use(a)
}<caret>
}
}
fun create(): String = ""
@@ -1,8 +1,8 @@
// WITH_RUNTIME
fun test() {
val x = when (val a = create()) {
val x = when<caret> (val a = create()) {
else -> use(a)
}<caret>
}
}
fun create(): String = ""