[kotlin] Redundant suppression: fix wrong applicability range with comments

^KTIJ-24678 Fixed

closes https://github.com/JetBrains/intellij-community/pull/2328

GitOrigin-RevId: 357a033509e7e6620c4e37bd7132f78cd58c9a3b
This commit is contained in:
Toshiaki Kameyama
2023-02-17 09:13:42 +09:00
committed by intellij-monorepo-bot
parent d8349cf18f
commit e58d5278ba
4 changed files with 42 additions and 0 deletions

View File

@@ -5,12 +5,15 @@ package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.daemon.QuickFixBundle
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.util.isAncestor
import com.intellij.psi.util.parentOfType
import org.jetbrains.kotlin.caches.resolve.KotlinCacheService
import org.jetbrains.kotlin.diagnostics.Severity
import org.jetbrains.kotlin.idea.base.psi.KotlinPsiHeuristics
import org.jetbrains.kotlin.idea.base.psi.textRangeIn
import org.jetbrains.kotlin.idea.highlighter.createSuppressWarningActions
import org.jetbrains.kotlin.idea.util.findSingleLiteralStringTemplateText
import org.jetbrains.kotlin.psi.KtAnnotated
@@ -56,6 +59,13 @@ class KotlinInspectionSuppressor : InspectionSuppressor, RedundantSuppressionDet
override fun isSuppressionFor(elementWithSuppression: PsiElement, place: PsiElement, toolId: String): Boolean {
return elementWithSuppression === place || elementWithSuppression.isAncestor(place, false)
}
override fun getHighlightingRange(elementWithSuppression: PsiElement, toolId: String): TextRange? {
val annotated = elementWithSuppression as? KtAnnotated ?: return null
val suppressAnnotationEntry = KotlinPsiHeuristics.findSuppressAnnotation(annotated) ?: return null
val index = StringUtil.indexOfIgnoreCase(suppressAnnotationEntry.text, toolId, 0).takeIf { it >= 0 } ?: return null
return TextRange(index, index + toolId.length).shiftRight(suppressAnnotationEntry.textRangeIn(annotated).startOffset)
}
}
private class RemoveRedundantSuppression(private val toolId: String) : LocalQuickFix {

View File

@@ -153,6 +153,11 @@ public abstract class HighlightingTestGenerated extends AbstractHighlightingTest
public void testUnused() throws Exception {
runTest("testData/highlighter/suppress/Unused.kt");
}
@TestMetadata("WithComment.kt")
public void testWithComment() throws Exception {
runTest("testData/highlighter/suppress/WithComment.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)

View File

@@ -0,0 +1,22 @@
// MoveVariableDeclarationIntoWhen
@Suppress("<warning descr="Redundant suppression">MoveVariableDeclarationIntoWhen</warning>", "unused")
class C
// MoveVariableDeclarationIntoWhen
@Suppress("unused", "<warning descr="Redundant suppression">MoveVariableDeclarationIntoWhen</warning>")
fun f() = 1
val v: Int
// MoveVariableDeclarationIntoWhen
@Suppress("<warning descr="Redundant suppression">MoveVariableDeclarationIntoWhen</warning>")
get() = 1
@Ann("MoveVariableDeclarationIntoWhen")
@Suppress("<warning descr="Redundant suppression">MoveVariableDeclarationIntoWhen</warning>")
class C2
annotation class Ann(val s: String)
// NO_CHECK_INFOS
// TOOL: com.intellij.codeInspection.RedundantSuppressInspection
// TOOL: org.jetbrains.kotlin.idea.inspections.MoveVariableDeclarationIntoWhenInspection

View File

@@ -153,6 +153,11 @@ public abstract class PerformanceHighlightingTestGenerated extends AbstractPerfo
public void testUnused() throws Exception {
runTest("../idea/tests/testData/highlighter/suppress/Unused.kt");
}
@TestMetadata("WithComment.kt")
public void testWithComment() throws Exception {
runTest("../idea/tests/testData/highlighter/suppress/WithComment.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)