Repository: cqfn/diKTat
Pull request #973 (2021-07-09 11:55:51)

Bugfix in COMPACT_OBJECT_INITIALIZATION
### What's done:
* Fix big in COMPACT_OBJECT_INITIALIZATION: Previously `apply` block could shadowed the assignment label, if it contain `this` keyword, such behavior led to the incorrect code. For now such cases will be ignored.
* Add test

codecov[bot]: # [Codecov](https://codecov.io/gh/cqfn/diKTat/pull/973?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn) Report
> Merging [#973](https://codecov.io/gh/cqfn/diKTat/pull/973?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn) (d778e00) into [master](https://codecov.io/gh/cqfn/diKTat/commit/70c5b9785f7bd0510b73f3efa5e1fb310ebaa830?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn) (70c5b97) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head d778e00 differs from pull request most recent head 2bd75dc. Consider uploading reports for the commit 2bd75dc to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/cqfn/diKTat/pull/973/graphs/tree.svg?width=650&height=150&src=pr&token=P2quRDffpt&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn)](https://codecov.io/gh/cqfn/diKTat/pull/973?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn)

```diff
@@            Coverage Diff            @@
##             master     #973   +/-   ##
=========================================
  Coverage     83.86%   83.87%           
  Complexity     2399     2399           
=========================================
  Files           101      101           
  Lines          6063     6065    +2     
  Branches       1790     1790           
=========================================
+ Hits           5085     5087    +2     
  Misses          268      268           
  Partials        710      710           
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `83.87% <100.00%> (+<0.01%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/cqfn/diKTat/pull/973?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn) | Coverage Δ | |
|---|---|---|
| [...et/rules/chapter6/classes/CompactInitialization.kt](https://codecov.io/gh/cqfn/diKTat/pull/973/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn#diff-ZGlrdGF0LXJ1bGVzL3NyYy9tYWluL2tvdGxpbi9vcmcvY3Fmbi9kaWt0YXQvcnVsZXNldC9ydWxlcy9jaGFwdGVyNi9jbGFzc2VzL0NvbXBhY3RJbml0aWFsaXphdGlvbi5rdA==) | `87.17% <100.00%> (+0.33%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/cqfn/diKTat/pull/973?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/cqfn/diKTat/pull/973?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn). Last update [70c5b97...2bd75dc](https://codecov.io/gh/cqfn/diKTat/pull/973?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn).


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 1ae3475e..b0ca67db 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
@@ -4,9 +4,14 @@ import org.cqfn.diktat.common.config.rules.RulesConfig
 import org.cqfn.diktat.ruleset.constants.Warnings.COMPACT_OBJECT_INITIALIZATION
 import org.cqfn.diktat.ruleset.rules.DiktatRule
 import org.cqfn.diktat.ruleset.utils.KotlinParser
+import org.cqfn.diktat.ruleset.utils.findLeafWithSpecificType
 import org.cqfn.diktat.ruleset.utils.getFunctionName
 
+import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT
+import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT
+import com.pinterest.ktlint.core.ast.ElementType.KDOC
 import com.pinterest.ktlint.core.ast.ElementType.LBRACE
+import com.pinterest.ktlint.core.ast.ElementType.THIS_KEYWORD
 import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
 import com.pinterest.ktlint.core.ast.isPartOfComment
 import org.jetbrains.kotlin.com.intellij.lang.ASTNode
@@ -63,6 +68,9 @@ class CompactInitialization(configRules: List<RulesConfig>) : DiktatRule(
                     it as KtBinaryExpression to (it.left as KtDotQualifiedExpression).selectorExpression!!
                 }
         }
+            .filter { (assignment, _) ->
+                assignment.node.findLeafWithSpecificType(THIS_KEYWORD) == null
+            }
             .toList()
             .forEach { (assignment, field) ->
                 COMPACT_OBJECT_INITIALIZATION.warnAndFix(
@@ -94,7 +102,7 @@ class CompactInitialization(configRules: List<RulesConfig>) : DiktatRule(
                     assignment
                         .node
                         .siblings(forward = false)
-                        .takeWhile { it != property.node }
+                        .takeWhile { it.elementType in listOf(WHITE_SPACE, EOL_COMMENT, BLOCK_COMMENT, KDOC) }
                         .toList()
                         .reversed()
                         .forEachIndexed { index, it ->


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/CompactInitializationFixTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/CompactInitializationFixTest.kt
index 32200570..6314741b 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/CompactInitializationFixTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/CompactInitializationFixTest.kt
@@ -25,4 +25,10 @@ class CompactInitializationFixTest : FixTestBase("test/chapter6/compact_initiali
     fun `should wrap properties into apply - existing apply with value argument`() {
         fixAndCompare("ApplyWithValueArgumentExpected.kt", "ApplyWithValueArgumentTest.kt")
     }
+
+    @Test
+    @Tag(WarningNames.COMPACT_OBJECT_INITIALIZATION)
+    fun `should not move statements with this keyword into apply block`() {
+        fixAndCompare("ApplyOnStatementsWithThisKeywordExpected.kt", "ApplyOnStatementsWithThisKeywordTest.kt")
+    }
 }
diff --git a/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ApplyOnStatementsWithThisKeywordExpected.kt b/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ApplyOnStatementsWithThisKeywordExpected.kt
new file mode 100644
index 00000000..8bf28487
--- /dev/null
+++ b/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ApplyOnStatementsWithThisKeywordExpected.kt
@@ -0,0 +1,14 @@
+fun String.createPluginConfig() {
+    val pluginConfig = TomlDecoder.decode<T>(
+        serializer(),
+        fakeFileNode,
+        DecoderConf()
+    ).apply {
+    prop1 = property1
+    // comment2
+    prop2 = property2}
+    pluginConfig.configLocation = this.toPath()
+    // comment1
+    pluginConfig.configLocation2 = this.toPath()
+}
+
diff --git a/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ApplyOnStatementsWithThisKeywordTest.kt b/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ApplyOnStatementsWithThisKeywordTest.kt
new file mode 100644
index 00000000..ca0980bd
--- /dev/null
+++ b/diktat-rules/src/test/resources/test/chapter6/compact_initialization/ApplyOnStatementsWithThisKeywordTest.kt
@@ -0,0 +1,14 @@
+fun String.createPluginConfig() {
+    val pluginConfig = TomlDecoder.decode<T>(
+        serializer(),
+        fakeFileNode,
+        DecoderConf()
+    )
+    pluginConfig.configLocation = this.toPath()
+    pluginConfig.prop1 = property1
+    // comment1
+    pluginConfig.configLocation2 = this.toPath()
+    // comment2
+    pluginConfig.prop2 = property2
+}
+