Repository: analysis-dev/diktat (kotlin)

Issue:
[FP] CommentedCode when text inside block comment may be interpreted as KDoc
## Describe the bug
```
/*
 * some text here
maybe even with another line
 */
```
causes
`[COMMENTED_OUT_CODE] you should not comment out code, use VCS to save it in history and delete this block: /* (cannot be auto-corrected)`

## Environment information
* diktat version: 0.1.7

Pull request description:
### What's done:
  * Fixed bug
  * Added tests

The bug was in the way block comments opening and closing tokens were removed

This pull request closes #652

Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/comments/CommentsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/comments/CommentsRule.kt
index 2be92b7709..caaf6fdcba 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/comments/CommentsRule.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/comments/CommentsRule.kt
@@ -56,7 +56,7 @@ class CommentsRule(private val configRules: List<RulesConfig>) : Rule("comments"
         val eolCommentsOffsetToText = getOffsetsToTextBlocksFromEolComments(node)
         val blockCommentsOffsetToText = node
             .findAllNodesWithSpecificType(BLOCK_COMMENT)
-            .map { it.startOffset to it.text.removeSurrounding("/*", "*/") }
+            .map { it.startOffset to it.text.trim().removeSurrounding("/*", "*/") }
 
         (eolCommentsOffsetToText + blockCommentsOffsetToText)
             .flatMap { (offset, text) ->


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt
index e7a0b8603e..ff65a11980 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/comments/CommentedCodeTest.kt
@@ -79,7 +79,7 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) {
 
     @Test
     @Tag(WarningNames.COMMENTED_OUT_CODE)
-    fun `Should warn if commented out function is detected (single line comments)`() {
+    fun `Should warn if commented out function is detected single line comments`() {
         lintMethod(
             """
            |import org.junit.Test
@@ -256,4 +256,41 @@ class CommentedCodeTest : LintTestBase(::CommentsRule) {
             """.trimMargin(),
             LintError(1, 1, ruleId, "${COMMENTED_OUT_CODE.warnText()} public fun someFunc(name: String): Boolean =", false))
     }
+
+    @Test
+    @Tag(WarningNames.COMMENTED_OUT_CODE)
+    fun `should not trigger on multiline comments #1`() {
+        lintMethod(
+            """
+            |/*
+            |
+            |   Copyright 2018-2020 John Doe.
+            |   
+            |   Licensed under the Apache License, Version 2.0 (the "License");
+            |   you may not use this file except in compliance with the License.
+            |   You may obtain a copy of the License at
+            |   
+            |       http://www.apache.org/licenses/LICENSE-2.0
+            |       
+            |   Unless required by applicable law or agreed to in writing, software
+            |   distributed under the License is distributed on an "AS IS" BASIS,
+            |   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+            |   See the License for the specific language governing permissions and
+            |   limitations under the License.
+            |   
+            |*/
+            """.trimMargin())
+    }
+
+    @Test
+    @Tag(WarningNames.COMMENTED_OUT_CODE)
+    fun `should not trigger on multiline comments #2`() {
+        lintMethod(
+            """
+            |   /*
+            |   * some text here
+            |   maybe even with another line
+            |   */
+            """.trimMargin())
+    }
 }
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt
index 59f3191b01..d0c5cbc2cb 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt
@@ -20,6 +20,7 @@ import com.charleskorn.kaml.Yaml
 import com.charleskorn.kaml.YamlConfiguration
 import com.pinterest.ktlint.core.LintError
 import org.junit.jupiter.api.AfterEach
+import org.junit.jupiter.api.Assertions
 import org.junit.jupiter.api.BeforeEach
 import org.junit.jupiter.api.Tag
 import org.junit.jupiter.api.Test
@@ -28,6 +29,8 @@ import java.io.File
 
 import kotlinx.serialization.encodeToString
 
+typealias ruleToConfig = Map<String, Map<String, String>>
+
 /**
  * Test for [DiktatRuleSetProvider] in autocorrect mode as a whole. All rules are applied to a file.
  * Note: ktlint uses initial text from a file to calculate line and column from offset. Because of that line/col of unfixed errors
@@ -44,7 +47,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
      * Disable some of the rules.
      */
     @Suppress("UnsafeCallOnNullableType")
-    private fun overrideRulesConfig(rulesToDisable: List<Warnings>) {
+    private fun overrideRulesConfig(rulesToDisable: List<Warnings>, rulesToOverride: ruleToConfig = emptyMap()) {
         val rulesConfig = RulesConfigReader(javaClass.classLoader).readResource(configFilePath)!!
             .toMutableList()
             .also { rulesConfig ->
@@ -52,6 +55,10 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
                     rulesConfig.removeIf { it.name == warning.name }
                     rulesConfig.add(RulesConfig(warning.name, enabled = false, configuration = emptyMap()))
                 }
+                rulesToOverride.forEach { (name, configuration) ->
+                    rulesConfig.removeIf { it.name == name }
+                    rulesConfig.add(RulesConfig(name, enabled = true, configuration = configuration))
+                }
             }
         createTempFile()
             .also {
@@ -88,6 +95,30 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
         fixAndCompare("DefaultPackageExpected.kt", "DefaultPackageTest.kt")
     }
 
+    @Test
+    @Tag("DiktatRuleSetProvider")
+    fun `smoke test #5`() {
+        overrideRulesConfig(emptyList(),
+            mapOf(
+                Warnings.HEADER_MISSING_OR_WRONG_COPYRIGHT.name to mapOf(
+                    "isCopyrightMandatory" to "true",
+                    "copyrightText" to """|Copyright 2018-2020 John Doe.
+                                    |    Licensed under the Apache License, Version 2.0 (the "License");
+                                    |    you may not use this file except in compliance with the License.
+                                    |    You may obtain a copy of the License at
+                                    |
+                                    |        http://www.apache.org/licenses/LICENSE-2.0
+                                """.trimMargin()
+                )
+            )
+        )
+        fixAndCompare("Example5Expected.kt", "Example5Test.kt")
+
+        Assertions.assertFalse(
+            unfixedLintErrors.contains(LintError(line = 1, col = 1, ruleId = "diktat-ruleset:comments", detail = "${Warnings.COMMENTED_OUT_CODE.warnText()} /*"))
+        )
+    }
+
     @Test
     @Tag("DiktatRuleSetProvider")
     fun `smoke test #4`() {
diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example5Expected.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example5Expected.kt
new file mode 100644
index 0000000000..9a8c5546fc
--- /dev/null
+++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example5Expected.kt
@@ -0,0 +1,15 @@
+/*
+    Copyright 2018-2020 John Doe.
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0
+*/
+
+package org.cqfn.diktat
+
+class Some {
+
+}
+
diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example5Test.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example5Test.kt
new file mode 100644
index 0000000000..7396489e1a
--- /dev/null
+++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example5Test.kt
@@ -0,0 +1,5 @@
+package org.cqfn.diktat
+
+class Some {
+
+}