ControlFlowWithEmptyBodyInspection: shouldn't report on while with comments
#KT-32419 Fixed
This commit is contained in:
+5
-2
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.inspections
|
|||||||
import com.intellij.codeInspection.ProblemsHolder
|
import com.intellij.codeInspection.ProblemsHolder
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.idea.inspections.collections.isCalling
|
import org.jetbrains.kotlin.idea.inspections.collections.isCalling
|
||||||
|
import org.jetbrains.kotlin.idea.util.hasComments
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
@@ -39,14 +40,16 @@ class ControlFlowWithEmptyBodyInspection : AbstractKotlinInspection() {
|
|||||||
|
|
||||||
override fun visitWhileExpression(expression: KtWhileExpression) {
|
override fun visitWhileExpression(expression: KtWhileExpression) {
|
||||||
val keyword = expression.allChildren.firstOrNull { it.node.elementType == KtTokens.WHILE_KEYWORD } ?: return
|
val keyword = expression.allChildren.firstOrNull { it.node.elementType == KtTokens.WHILE_KEYWORD } ?: return
|
||||||
if (expression.body.isEmptyBodyOrNull()) {
|
val body = expression.body
|
||||||
|
if (body?.hasComments() != true && body.isEmptyBodyOrNull()) {
|
||||||
holder.registerProblem(expression, keyword)
|
holder.registerProblem(expression, keyword)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitDoWhileExpression(expression: KtDoWhileExpression) {
|
override fun visitDoWhileExpression(expression: KtDoWhileExpression) {
|
||||||
val keyword = expression.allChildren.firstOrNull { it.node.elementType == KtTokens.DO_KEYWORD } ?: return
|
val keyword = expression.allChildren.firstOrNull { it.node.elementType == KtTokens.DO_KEYWORD } ?: return
|
||||||
if (expression.body.isEmptyBodyOrNull()) {
|
val body = expression.body
|
||||||
|
if (body?.hasComments() != true && body.isEmptyBodyOrNull()) {
|
||||||
holder.registerProblem(expression, keyword)
|
holder.registerProblem(expression, keyword)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-12
@@ -10,8 +10,6 @@ import com.intellij.codeInspection.ProblemHighlightType
|
|||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.PsiComment
|
|
||||||
import com.intellij.psi.PsiElement
|
|
||||||
import com.intellij.psi.PsiWhiteSpace
|
import com.intellij.psi.PsiWhiteSpace
|
||||||
import com.intellij.psi.search.LocalSearchScope
|
import com.intellij.psi.search.LocalSearchScope
|
||||||
import com.intellij.psi.search.searches.ReferencesSearch
|
import com.intellij.psi.search.searches.ReferencesSearch
|
||||||
@@ -25,6 +23,7 @@ import org.jetbrains.kotlin.idea.intentions.branchedTransformations.expressionCo
|
|||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.fromIfKeywordToRightParenthesisTextRangeInThis
|
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.fromIfKeywordToRightParenthesisTextRangeInThis
|
||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.shouldBeTransformed
|
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.shouldBeTransformed
|
||||||
import org.jetbrains.kotlin.idea.util.CommentSaver
|
import org.jetbrains.kotlin.idea.util.CommentSaver
|
||||||
|
import org.jetbrains.kotlin.idea.util.hasComments
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange
|
import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange
|
||||||
@@ -102,16 +101,6 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti
|
|||||||
return newElvis
|
return newElvis
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun PsiElement.hasComments(): Boolean {
|
|
||||||
var result = false
|
|
||||||
accept(object : KtTreeVisitorVoid() {
|
|
||||||
override fun visitComment(comment: PsiComment?) {
|
|
||||||
result = true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun calcData(ifExpression: KtIfExpression): Data? {
|
private fun calcData(ifExpression: KtIfExpression): Data? {
|
||||||
if (ifExpression.`else` != null) return null
|
if (ifExpression.`else` != null) return null
|
||||||
|
|
||||||
|
|||||||
@@ -6,11 +6,13 @@
|
|||||||
package org.jetbrains.kotlin.idea.util
|
package org.jetbrains.kotlin.idea.util
|
||||||
|
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import com.intellij.psi.PsiComment
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.idea.intentions.callExpression
|
import org.jetbrains.kotlin.idea.intentions.callExpression
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.containingClass
|
import org.jetbrains.kotlin.psi.psiUtil.containingClass
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||||
@@ -42,4 +44,6 @@ fun PsiElement.textRangeIn(other: PsiElement): TextRange = textRange.shiftLeft(o
|
|||||||
|
|
||||||
fun KtDotQualifiedExpression.calleeTextRangeInThis(): TextRange? = callExpression?.calleeExpression?.textRangeIn(this)
|
fun KtDotQualifiedExpression.calleeTextRangeInThis(): TextRange? = callExpression?.calleeExpression?.textRangeIn(this)
|
||||||
|
|
||||||
fun KtNamedDeclaration.nameIdentifierTextRangeInThis(): TextRange? = nameIdentifier?.textRangeIn(this)
|
fun KtNamedDeclaration.nameIdentifierTextRangeInThis(): TextRange? = nameIdentifier?.textRangeIn(this)
|
||||||
|
|
||||||
|
fun PsiElement.hasComments(): Boolean = anyDescendantOfType<PsiComment>()
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
// PROBLEM: 'do while' has empty body
|
// PROBLEM: none
|
||||||
// FIX: none
|
// FIX: none
|
||||||
|
|
||||||
fun test(i: Int) {
|
fun test(i: Int) {
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
// PROBLEM: 'while' has empty body
|
// PROBLEM: none
|
||||||
// FIX: none
|
// FIX: none
|
||||||
|
|
||||||
fun test(i: Int) {
|
fun test(i: Int) {
|
||||||
|
|||||||
Reference in New Issue
Block a user