diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ControlFlowWithEmptyBodyInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ControlFlowWithEmptyBodyInspection.kt index c2f2a1438c6..ed9bc3279b5 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ControlFlowWithEmptyBodyInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ControlFlowWithEmptyBodyInspection.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.inspections import com.intellij.codeInspection.ProblemsHolder import com.intellij.psi.PsiElement 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.name.FqName import org.jetbrains.kotlin.psi.* @@ -39,14 +40,16 @@ class ControlFlowWithEmptyBodyInspection : AbstractKotlinInspection() { override fun visitWhileExpression(expression: KtWhileExpression) { 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) } } override fun visitDoWhileExpression(expression: KtDoWhileExpression) { 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) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt index 20792291833..d7f2a6b8bc8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt @@ -10,8 +10,6 @@ import com.intellij.codeInspection.ProblemHighlightType import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project 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.search.LocalSearchScope 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.shouldBeTransformed import org.jetbrains.kotlin.idea.util.CommentSaver +import org.jetbrains.kotlin.idea.util.hasComments import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange @@ -102,16 +101,6 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti 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? { if (ifExpression.`else` != null) return null diff --git a/idea/src/org/jetbrains/kotlin/idea/util/expressionExt.kt b/idea/src/org/jetbrains/kotlin/idea/util/expressionExt.kt index 0ac1ba98f38..e8b3195da15 100644 --- a/idea/src/org/jetbrains/kotlin/idea/util/expressionExt.kt +++ b/idea/src/org/jetbrains/kotlin/idea/util/expressionExt.kt @@ -6,11 +6,13 @@ package org.jetbrains.kotlin.idea.util import com.intellij.openapi.util.TextRange +import com.intellij.psi.PsiComment import com.intellij.psi.PsiElement import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.intentions.callExpression import org.jetbrains.kotlin.lexer.KtTokens 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.startOffset 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 KtNamedDeclaration.nameIdentifierTextRangeInThis(): TextRange? = nameIdentifier?.textRangeIn(this) \ No newline at end of file +fun KtNamedDeclaration.nameIdentifierTextRangeInThis(): TextRange? = nameIdentifier?.textRangeIn(this) + +fun PsiElement.hasComments(): Boolean = anyDescendantOfType() \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/controlFlowWithEmptyBody/doWhile/blockHasComment.kt b/idea/testData/inspectionsLocal/controlFlowWithEmptyBody/doWhile/blockHasComment.kt index 1198c8db41e..02bc30f42c7 100644 --- a/idea/testData/inspectionsLocal/controlFlowWithEmptyBody/doWhile/blockHasComment.kt +++ b/idea/testData/inspectionsLocal/controlFlowWithEmptyBody/doWhile/blockHasComment.kt @@ -1,4 +1,4 @@ -// PROBLEM: 'do while' has empty body +// PROBLEM: none // FIX: none fun test(i: Int) { diff --git a/idea/testData/inspectionsLocal/controlFlowWithEmptyBody/while/blockHasComment.kt b/idea/testData/inspectionsLocal/controlFlowWithEmptyBody/while/blockHasComment.kt index 4f815e1d74e..486f2cce274 100644 --- a/idea/testData/inspectionsLocal/controlFlowWithEmptyBody/while/blockHasComment.kt +++ b/idea/testData/inspectionsLocal/controlFlowWithEmptyBody/while/blockHasComment.kt @@ -1,4 +1,4 @@ -// PROBLEM: 'while' has empty body +// PROBLEM: none // FIX: none fun test(i: Int) {