From 4dc92cf3fa1e626b85d8e0fa2fa18000878f94ef Mon Sep 17 00:00:00 2001 From: "Victoria.Petrakovich" Date: Mon, 4 Nov 2024 19:52:51 +0100 Subject: [PATCH] [kotlin] Don't suggest using property access syntax in `super.` calls KTIJ-31647 K2: False positive "property access syntax" on `super` setters and getters (cherry picked from commit e0bc11426631dab4fc5f30e5cbff5fbdba3fe395) IJ-CR-148581 GitOrigin-RevId: 4059ff4377fd11be056ee63c8cafea6d39e0af79 --- .../UsePropertyAccessSyntaxInspection.kt | 8 +++++++- .../tests/K2LocalInspectionTestGenerated.java | 15 ++++++++++----- .../inspections/LocalInspectionTestGenerated.java | 15 ++++++++++----- ...Call.kt => dontReplaceGetterAfterSuperCall.kt} | 4 +--- .../dontReplaceWhenReceiverIsSuper.1.java | 13 +++++++++++++ .../dontReplaceWhenReceiverIsSuper.kt | 8 ++++++++ .../replaceGetterAfterSuperCall.kt.after | 11 ----------- 7 files changed, 49 insertions(+), 25 deletions(-) rename plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/{replaceGetterAfterSuperCall.kt => dontReplaceGetterAfterSuperCall.kt} (58%) create mode 100644 plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceWhenReceiverIsSuper.1.java create mode 100644 plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceWhenReceiverIsSuper.kt delete mode 100644 plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterAfterSuperCall.kt.after diff --git a/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/UsePropertyAccessSyntaxInspection.kt b/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/UsePropertyAccessSyntaxInspection.kt index 2d5bd2c42260..74e9f3c797bf 100644 --- a/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/UsePropertyAccessSyntaxInspection.kt +++ b/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/UsePropertyAccessSyntaxInspection.kt @@ -165,6 +165,12 @@ class UsePropertyAccessSyntaxInspection : LocalInspectionTool(), CleanupLocalIns return } + if (expressionParent is KtDotQualifiedExpression) { + if (expressionParent.receiverExpression is KtSuperExpression) { + return // Shouldn't suggest property accessors on "super" + } + } + if (propertyAccessorKind is PropertyAccessorKind.Setter) { if (expressionParent is KtDotQualifiedExpression) { if (expressionParent.parent is KtDotQualifiedExpression) { @@ -194,7 +200,7 @@ class UsePropertyAccessSyntaxInspection : LocalInspectionTool(), CleanupLocalIns val returnType = successfulFunctionCallSymbol.returnType.lowerBoundIfFlexible() - // For extension functions, receiver type taken such way is is null + // For extension functions, receiver type taken such way is null val receiverType = resolvedFunctionCall.partiallyAppliedSymbol.dispatchReceiver?.type?.lowerBoundIfFlexible() ?: return val syntheticProperty = getSyntheticProperty(propertyNames, receiverType) ?: return diff --git a/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2LocalInspectionTestGenerated.java b/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2LocalInspectionTestGenerated.java index af401b3b5455..0d8f1b55a9bc 100644 --- a/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2LocalInspectionTestGenerated.java +++ b/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2LocalInspectionTestGenerated.java @@ -6350,6 +6350,11 @@ public abstract class K2LocalInspectionTestGenerated extends AbstractK2LocalInsp runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceFirstSetterInChain.kt"); } + @TestMetadata("dontReplaceGetterAfterSuperCall.kt") + public void testDontReplaceGetterAfterSuperCall() throws Exception { + runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceGetterAfterSuperCall.kt"); + } + @TestMetadata("dontReplaceGetterFromKotlinFile.kt") public void testDontReplaceGetterFromKotlinFile() throws Exception { runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceGetterFromKotlinFile.kt"); @@ -6460,6 +6465,11 @@ public abstract class K2LocalInspectionTestGenerated extends AbstractK2LocalInsp runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceSetterWithReservedWord2.kt"); } + @TestMetadata("dontReplaceWhenReceiverIsSuper.kt") + public void testDontReplaceWhenReceiverIsSuper() throws Exception { + runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceWhenReceiverIsSuper.kt"); + } + @TestMetadata("get.kt") public void testGet() throws Exception { runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/get.kt"); @@ -6485,11 +6495,6 @@ public abstract class K2LocalInspectionTestGenerated extends AbstractK2LocalInsp runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterAfterSafeCall.kt"); } - @TestMetadata("replaceGetterAfterSuperCall.kt") - public void testReplaceGetterAfterSuperCall() throws Exception { - runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterAfterSuperCall.kt"); - } - @TestMetadata("replaceGetterFromGenericClassMethod.kt") public void testReplaceGetterFromGenericClassMethod() throws Exception { runTest("../../../idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterFromGenericClassMethod.kt"); diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index a4b7eac36bb8..3df35c4e12dc 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -19272,6 +19272,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceFirstSetterInChain.kt"); } + @TestMetadata("dontReplaceGetterAfterSuperCall.kt") + public void testDontReplaceGetterAfterSuperCall() throws Exception { + runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceGetterAfterSuperCall.kt"); + } + @TestMetadata("dontReplaceGetterFromKotlinFile.kt") public void testDontReplaceGetterFromKotlinFile() throws Exception { runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceGetterFromKotlinFile.kt"); @@ -19382,6 +19387,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceSetterWithReservedWord2.kt"); } + @TestMetadata("dontReplaceWhenReceiverIsSuper.kt") + public void testDontReplaceWhenReceiverIsSuper() throws Exception { + runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceWhenReceiverIsSuper.kt"); + } + @TestMetadata("get.kt") public void testGet() throws Exception { runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/get.kt"); @@ -19407,11 +19417,6 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterAfterSafeCall.kt"); } - @TestMetadata("replaceGetterAfterSuperCall.kt") - public void testReplaceGetterAfterSuperCall() throws Exception { - runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterAfterSuperCall.kt"); - } - @TestMetadata("replaceGetterFromGenericClassMethod.kt") public void testReplaceGetterFromGenericClassMethod() throws Exception { runTest("testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterFromGenericClassMethod.kt"); diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterAfterSuperCall.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceGetterAfterSuperCall.kt similarity index 58% rename from plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterAfterSuperCall.kt rename to plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceGetterAfterSuperCall.kt index 7f9a9c7db3ed..dce89b2c3284 100644 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterAfterSuperCall.kt +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceGetterAfterSuperCall.kt @@ -1,7 +1,5 @@ // WITH_STDLIB -// FIX: Use property access syntax -// IGNORE_K1 -// PROBLEM: "Use of getter method instead of property access syntax" +// PROBLEM: none import java.io.File class MyFile : File("file") { diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceWhenReceiverIsSuper.1.java b/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceWhenReceiverIsSuper.1.java new file mode 100644 index 000000000000..1ef5dfd386d3 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceWhenReceiverIsSuper.1.java @@ -0,0 +1,13 @@ +public class Foo { + + public void setFoo(int x) { + this.x = x; + } + + private int x = 0; + + public int getFoo(){ + return x; + } + +} diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceWhenReceiverIsSuper.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceWhenReceiverIsSuper.kt new file mode 100644 index 000000000000..8323d0145a0d --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/dontReplaceWhenReceiverIsSuper.kt @@ -0,0 +1,8 @@ +// PROBLEM: none + +class KotlinInheritor: Foo() { + + override fun setFoo(x: Int) { + super.setFoo(x) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterAfterSuperCall.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterAfterSuperCall.kt.after deleted file mode 100644 index 2042632b53d8..000000000000 --- a/plugins/kotlin/idea/tests/testData/inspectionsLocal/usePropertyAccessSyntax/dotQualifiedExpressions/replaceGetterAfterSuperCall.kt.after +++ /dev/null @@ -1,11 +0,0 @@ -// WITH_STDLIB -// FIX: Use property access syntax -// IGNORE_K1 -// PROBLEM: "Use of getter method instead of property access syntax" -import java.io.File - -class MyFile : File("file") { - override fun getCanonicalFile(): File { - return super.canonicalFile - } -}