mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[kotlin] "Variable declaration could be moved inside 'when'": don't highlight cases when variable should be inlined
^KTIJ-25794 Fixed GitOrigin-RevId: c3b87a0502ae97a72510de43f47af792da4f9ff4
This commit is contained in:
committed by
intellij-monorepo-bot
parent
4d81c9c964
commit
c6e548488d
+13
-10
@@ -4,20 +4,20 @@ package org.jetbrains.kotlin.idea.inspections
|
||||
|
||||
import com.intellij.codeInsight.intention.FileModifier
|
||||
import com.intellij.codeInsight.intention.FileModifier.SafeFieldForPreview
|
||||
import com.intellij.codeInspection.CleanupLocalInspectionTool
|
||||
import com.intellij.codeInspection.LocalQuickFix
|
||||
import com.intellij.codeInspection.ProblemDescriptor
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.codeInspection.*
|
||||
import com.intellij.codeInspection.ProblemHighlightType.*
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.annotations.Nls
|
||||
import org.jetbrains.kotlin.idea.base.psi.isOneLiner
|
||||
import org.jetbrains.kotlin.idea.base.psi.textRangeIn
|
||||
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.classic.inspections.AbstractKotlinInspection
|
||||
import org.jetbrains.kotlin.idea.codeinsight.utils.findExistingEditor
|
||||
import org.jetbrains.kotlin.idea.core.moveCaret
|
||||
import org.jetbrains.kotlin.idea.inspections.Action.*
|
||||
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.countUsages
|
||||
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.previousStatement
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
@@ -35,13 +35,16 @@ class MoveVariableDeclarationIntoWhenInspection : AbstractKotlinInspection(), Cl
|
||||
if (initializer.isComplex()) return
|
||||
|
||||
val action = property.action(expression)
|
||||
if (action == Action.NOTHING) return
|
||||
if (action == Action.MOVE && !property.isOneLiner()) return
|
||||
if (action == NOTHING) return
|
||||
if (action == MOVE && !property.isOneLiner()) return
|
||||
|
||||
holder.registerProblem(
|
||||
val highlightType = if (action == INLINE) INFORMATION else GENERIC_ERROR_OR_WARNING
|
||||
holder.registerProblemWithoutOfflineInformation(
|
||||
property,
|
||||
TextRange.from(identifier.startOffsetInParent, identifier.textLength),
|
||||
action.description,
|
||||
isOnTheFly,
|
||||
highlightType,
|
||||
TextRange.from(identifier.startOffsetInParent, identifier.textLength),
|
||||
action.createFix(subjectExpression.createSmartPointer())
|
||||
)
|
||||
})
|
||||
@@ -78,8 +81,8 @@ private enum class Action {
|
||||
}
|
||||
|
||||
private fun KtProperty.action(element: KtElement): Action = when (val elementUsages = countUsages(element)) {
|
||||
countUsages() -> if (elementUsages == 1) Action.INLINE else Action.MOVE
|
||||
else -> Action.NOTHING
|
||||
countUsages() -> if (elementUsages == 1) INLINE else MOVE
|
||||
else -> NOTHING
|
||||
}
|
||||
|
||||
private fun KtWhenExpression.findDeclarationNear(): KtProperty? {
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun test() = true
|
||||
|
||||
fun foo(): Int {
|
||||
|
||||
+2
-1
@@ -1,7 +1,8 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun test() = true
|
||||
|
||||
fun foo(): Int {
|
||||
return when (<caret>test()) {
|
||||
return when (test()) {
|
||||
true -> 42
|
||||
else -> null
|
||||
} ?: 55
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun test() = true
|
||||
|
||||
fun foo() {
|
||||
|
||||
+2
-1
@@ -1,7 +1,8 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun test() = true
|
||||
|
||||
fun foo() {
|
||||
val b = !when (<caret>test()) {
|
||||
val b = !when (test()) {
|
||||
true -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun test() = 42
|
||||
|
||||
fun foo() {
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun test() = 42
|
||||
|
||||
fun foo() {
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun test(){
|
||||
/* aaa */
|
||||
val <caret>foo = 1 /* bbb */ /* ccc */
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun test(){
|
||||
/* aaa */
|
||||
when(1) { /* bbb */ /* ccc */
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun foo() {
|
||||
val a<caret> = 1
|
||||
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun foo() {
|
||||
// comment
|
||||
when (<caret>1) {
|
||||
when (1) {
|
||||
1 -> {
|
||||
}
|
||||
else -> {
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun foo(style: Int?) {
|
||||
val a<caret> = style // comment
|
||||
when (a) {
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun foo(style: Int?) {
|
||||
when (style) { // comment
|
||||
0 -> {}
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun test(){
|
||||
/** aaa */
|
||||
val <caret>foo = 1 /* bbb */
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun test(){
|
||||
// ccc
|
||||
/** aaa */
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun foo() {
|
||||
val a<caret> = 1
|
||||
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
fun foo() {
|
||||
when (1) {
|
||||
1 -> {
|
||||
|
||||
Reference in New Issue
Block a user