From 1be37bf052ec28c7540c92345889ee00892d90a1 Mon Sep 17 00:00:00 2001 From: Andrey Cherkasov Date: Tue, 10 Sep 2024 10:04:39 +0400 Subject: [PATCH] [kotlin] AddAnnotationUseSiteTargetFix: don't suggest quick fix when there are no applicable use-site targets ^KTIJ-29573 GitOrigin-RevId: 235ae43b1f99b68b5b11f1e80c68de73bf029b24 --- .../fixes/WrongAnnotationTargetFixFactories.kt | 4 +++- .../codeinsight/fixes/HighLevelQuickFixTestGenerated.java | 5 +++++ .../kotlin/idea/quickfix/K1QuickFixTestGenerated.java | 5 +++++ .../quickfix/addAnnotationUseSiteTarget/notApplicable.kt | 8 ++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 plugins/kotlin/idea/tests/testData/quickfix/addAnnotationUseSiteTarget/notApplicable.kt diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/WrongAnnotationTargetFixFactories.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/WrongAnnotationTargetFixFactories.kt index 658746cf83dd..78ec68aabdec 100644 --- a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/WrongAnnotationTargetFixFactories.kt +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/WrongAnnotationTargetFixFactories.kt @@ -15,7 +15,9 @@ import org.jetbrains.kotlin.psi.KtAnnotationEntry internal object WrongAnnotationTargetFixFactories { val addAnnotationUseSiteTargetFixFactory = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.WrongAnnotationTarget -> - val applicableUseSiteTargets = diagnostic.psi.getApplicableUseSiteTargets() + val applicableUseSiteTargets = diagnostic.psi.getApplicableUseSiteTargets().takeIf { + it.isNotEmpty() + } ?: return@ModCommandBased emptyList() listOfNotNull( AddAnnotationUseSiteTargetFix(diagnostic.psi, applicableUseSiteTargets) diff --git a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java index fbf71dff5569..b0867eb85ffb 100644 --- a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java +++ b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java @@ -5640,6 +5640,11 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu public void testGet() throws Exception { runTest("../../../idea/tests/testData/quickfix/addAnnotationUseSiteTarget/get.kt"); } + + @TestMetadata("notApplicable.kt") + public void testNotApplicable() throws Exception { + runTest("../../../idea/tests/testData/quickfix/addAnnotationUseSiteTarget/notApplicable.kt"); + } } diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java index 4dd01d10f4cc..0c9b19a76598 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java @@ -438,6 +438,11 @@ public abstract class K1QuickFixTestGenerated extends AbstractK1QuickFixTest { public void testGet() throws Exception { runTest("testData/quickfix/addAnnotationUseSiteTarget/get.kt"); } + + @TestMetadata("notApplicable.kt") + public void testNotApplicable() throws Exception { + runTest("testData/quickfix/addAnnotationUseSiteTarget/notApplicable.kt"); + } } @RunWith(JUnit3RunnerWithInners.class) diff --git a/plugins/kotlin/idea/tests/testData/quickfix/addAnnotationUseSiteTarget/notApplicable.kt b/plugins/kotlin/idea/tests/testData/quickfix/addAnnotationUseSiteTarget/notApplicable.kt new file mode 100644 index 000000000000..fe9013a8b63e --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/addAnnotationUseSiteTarget/notApplicable.kt @@ -0,0 +1,8 @@ +// "Add use-site target" "false" +// ERROR: This annotation is not applicable to target 'class' + +@Target(AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER) +annotation class Anno + +@Anno +class Foo \ No newline at end of file