Repository: analysis-dev/diktat (kotlin)

Issue:
Incorrect wrapping into `apply` function
## Describe the bug
```kotlin
fun fo() {
    val pair = it.split("=", limit = 2).map {
        it.replace("\\=", "=")
    }
    pair.first() to pair.last()
}
```

## Expected behavior:
Rule shouldn't trigger, because top-level node is an infix function, not dot-qualified call, from which we can extract receiver.
Also, it can potentially break in other cases, when result of dot-qualified call is implicitly returned. If we wrap it into `apply`, we will break the code, while wrapping into `run` will not.

## Observed behavior
```kotlin
fun fo() {
    val pair = it.split("=", limit = 2).map {
        it.replace("\\=", "=")
    }.apply {
        first() to pair.last()
    }
}
```

## Environment information
* diktat version: 1.0.0-rc.3

Pull request description:
### What's done:
* Make rule less strictly: trigger only on expressions, which contains equal sign
* Add tests

Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt
index 9a4e2dfe71..7ddffb07da 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/CompactInitialization.kt
@@ -11,8 +11,10 @@ import org.cqfn.diktat.ruleset.utils.getFunctionName
 import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT
 import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION
 import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT
+import com.pinterest.ktlint.core.ast.ElementType.EQ
 import com.pinterest.ktlint.core.ast.ElementType.KDOC
 import com.pinterest.ktlint.core.ast.ElementType.LBRACE
+import com.pinterest.ktlint.core.ast.ElementType.OPERATION_REFERENCE
 import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
 import com.pinterest.ktlint.core.ast.ElementType.THIS_KEYWORD
 import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
@@ -65,10 +67,11 @@ class CompactInitialization(configRules: List<RulesConfig>) : DiktatRule(
                 .filterNot { it.node.isPartOfComment() || it is PsiWhiteSpace }
                 .takeWhile {
                     // statements like `name.field = value` where name == propertyName
-                    it is KtBinaryExpression && (it.left as? KtDotQualifiedExpression)?.run {
-                        (receiverExpression as? KtNameReferenceExpression)?.getReferencedName() == propertyName
-                    }
-                        ?: false
+                    it is KtBinaryExpression && it.node.findChildByType(OPERATION_REFERENCE)?.findChildByType(EQ) != null &&
+                            (it.left as? KtDotQualifiedExpression)?.run {
+                                (receiverExpression as? KtNameReferenceExpression)?.getReferencedName() == propertyName
+                            }
+                                ?: false
                 }
                 .map {
                     // collect as an assignment associated with assigned field name


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/CompactInitializationWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/CompactInitializationWarnTest.kt
index 3f9999da94..a6890747e3 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/CompactInitializationWarnTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/CompactInitializationWarnTest.kt
@@ -91,4 +91,54 @@ class CompactInitializationWarnTest : LintTestBase(::CompactInitialization) {
             LintError(7, 5, ruleId, "${COMPACT_OBJECT_INITIALIZATION.warnText()} timeout", true)
         )
     }
+
+    // Creating the `apply` block here breaks the compilation
+    @Test
+    @Tag(WarningNames.COMPACT_OBJECT_INITIALIZATION)
+    fun `should not trigger to infix function 1`() {
+        lintMethod(
+            """
+                |fun foo(line: String) = line
+                |    .split(",", ", ")
+                |    .associate {
+                |        val pair = it.split("=", limit = 2).map {
+                |            it.replace("\\=", "=")
+                |        }
+                |        pair.first() to pair.last()
+                |    }
+            """.trimMargin(),
+        )
+    }
+
+    // Apply block doesn't break the compilation, however such changes can break the user logic
+    @Test
+    @Tag(WarningNames.COMPACT_OBJECT_INITIALIZATION)
+    fun `should not trigger to infix function 2`() {
+        lintMethod(
+            """
+                |fun foo(line: String) {
+                |    val pair = line.split("=", limit = 2).map {
+                |        it.replace("\\=", "=")
+                |    }
+                |    pair.first() to pair.last()
+                |}
+            """.trimMargin(),
+        )
+    }
+
+    // For generality don't trigger on any infix function, despite the fact, that with apply block all will be okay
+    @Test
+    @Tag(WarningNames.COMPACT_OBJECT_INITIALIZATION)
+    fun `should not trigger to infix function 3`() {
+        lintMethod(
+            """
+                |fun `translate text`() {
+                |    val res = translateText(text = "dummy")
+                |    (res is TranslationsSuccess) shouldBe true
+                |    val translationsSuccess = res as TranslationsSuccess
+                |    translationsSuccess.translations shouldHaveSize 1
+                |}
+            """.trimMargin(),
+        )
+    }
 }
diff --git a/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ParenthesizedReceiverExpected.kt b/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ParenthesizedReceiverExpected.kt
index 8c20c9d4ba..e1fb1a6357 100644
--- a/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ParenthesizedReceiverExpected.kt
+++ b/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ParenthesizedReceiverExpected.kt
@@ -2,5 +2,5 @@ fun `translate text`() {
     val res = translateText(text = "dummy")
     (res is TranslationsSuccess) shouldBe true
     val translationsSuccess = (res as TranslationsSuccess).apply {
-    translations shouldHaveSize 1}
+    translations = 1}
 }
diff --git a/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ParenthesizedReceiverTest.kt b/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ParenthesizedReceiverTest.kt
index d7eb1c12b3..26e34a6ecd 100644
--- a/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ParenthesizedReceiverTest.kt
+++ b/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ParenthesizedReceiverTest.kt
@@ -2,5 +2,5 @@ fun `translate text`() {
     val res = translateText(text = "dummy")
     (res is TranslationsSuccess) shouldBe true
     val translationsSuccess = res as TranslationsSuccess
-    translationsSuccess.translations shouldHaveSize 1
+    translationsSuccess.translations = 1
 }