diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/trackers/KotlinCodeBlockModificationListenerCompat.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/trackers/KotlinCodeBlockModificationListenerCompat.kt index e380efdd1d7..c80dbfc3846 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/trackers/KotlinCodeBlockModificationListenerCompat.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/trackers/KotlinCodeBlockModificationListenerCompat.kt @@ -222,13 +222,23 @@ abstract class KotlinCodeBlockModificationListenerCompat(protected val project: // } if (blockDeclaration.typeReference != null) { val accessors = - blockDeclaration.accessors.map { it.initializer ?: it.bodyExpression } + blockDeclaration.initializer - for (accessor in accessors) { + blockDeclaration.accessors.map { it.initializer ?: it.bodyExpression } + + val accessorList = if (blockDeclaration.initializer.isAncestor(element) && + // call expression changes in property initializer are OCB, see KT-38443 + KtPsiUtil.getTopmostParentOfTypes(element, KtCallExpression::class.java) == null + ) { + accessors + blockDeclaration.initializer + } else { + accessors + } + + for (accessor in accessorList) { accessor?.takeIf { - it.isAncestor(element) && - // adding annotations to accessor is the same as change contract of property - (element !is KtAnnotated || element.annotationEntries.isEmpty()) - } + it.isAncestor(element) && + // adding annotations to accessor is the same as change contract of property + (element !is KtAnnotated || element.annotationEntries.isEmpty()) + } ?.let { expression -> val declaration = KtPsiUtil.getTopmostParentOfTypes(blockDeclaration, KtClassOrObject::class.java) as? KtElement ?: diff --git a/idea/testData/codeInsight/outOfBlock/InClassPropertyInitializer.kt b/idea/testData/codeInsight/outOfBlock/InClassPropertyInitializer.kt new file mode 100644 index 00000000000..9f3d14731c6 --- /dev/null +++ b/idea/testData/codeInsight/outOfBlock/InClassPropertyInitializer.kt @@ -0,0 +1,10 @@ +// OUT_OF_CODE_BLOCK: TRUE +// ERROR: No value passed for parameter 'i2' +// TYPE: '\b\b' +class Test { + val foo = bar(1, 2) + + fun bar(i: Int, i2: Int) = "" +} + +// TODO: it has to be non OCB ! \ No newline at end of file diff --git a/idea/testData/codeInsight/outOfBlock/InClassPropertyInitializerWithoutInference.kt b/idea/testData/codeInsight/outOfBlock/InClassPropertyInitializerWithoutInference.kt new file mode 100644 index 00000000000..5f30432ccad --- /dev/null +++ b/idea/testData/codeInsight/outOfBlock/InClassPropertyInitializerWithoutInference.kt @@ -0,0 +1,11 @@ +// TODO: it has to be non OCB ! +// OUT_OF_CODE_BLOCK: TRUE +// ERROR: No value passed for parameter 'i2' +// TYPE: '\b\b' +class Test { + val foo: String? = "a" + bar(1, 2) + + fun bar(i: Int, i2: Int) = "" +} + +// SKIP_ANALYZE_CHECK diff --git a/idea/testData/codeInsight/outOfBlock/InClassPropertyInitializerWithoutInference2.kt b/idea/testData/codeInsight/outOfBlock/InClassPropertyInitializerWithoutInference2.kt new file mode 100644 index 00000000000..c43483ecfca --- /dev/null +++ b/idea/testData/codeInsight/outOfBlock/InClassPropertyInitializerWithoutInference2.kt @@ -0,0 +1,11 @@ +// TODO: it has to be non OCB ! +// OUT_OF_CODE_BLOCK: TRUE +// ERROR: The integer literal does not conform to the expected type String? +// TYPE: '\b\b\b\b' +class Test { + val foo: String? = "a"+1 + + fun bar(i: Int, i2: Int) = "" +} + +// SKIP_ANALYZE_CHECK diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/OutOfBlockModificationTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/OutOfBlockModificationTestGenerated.java index 5e20aca1e73..cacbd20a4c1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/OutOfBlockModificationTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/OutOfBlockModificationTestGenerated.java @@ -113,6 +113,21 @@ public class OutOfBlockModificationTestGenerated extends AbstractOutOfBlockModif runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyAccessor.kt"); } + @TestMetadata("InClassPropertyInitializer.kt") + public void testInClassPropertyInitializer() throws Exception { + runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyInitializer.kt"); + } + + @TestMetadata("InClassPropertyInitializerWithoutInference.kt") + public void testInClassPropertyInitializerWithoutInference() throws Exception { + runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyInitializerWithoutInference.kt"); + } + + @TestMetadata("InClassPropertyInitializerWithoutInference2.kt") + public void testInClassPropertyInitializerWithoutInference2() throws Exception { + runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyInitializerWithoutInference2.kt"); + } + @TestMetadata("InComment.kt") public void testInComment() throws Exception { runTest("idea/testData/codeInsight/outOfBlock/InComment.kt");