Repository: analysis-dev/diktat (kotlin)

Issue:
KDoc for private properties are added only when there was no KDoc (also warning)
This code
```kotlin
class Example(private val foo: Int)
```
is autofixed into
```
/**
 * @property foo
 */
class Example(private val foo: Int)
```
while this
```kotlin
/**
 * KDoc
 */
class Example(private val foo: Int)
```
doesn't.
The first snippet shouldn't be corrected too.

This is not only about the fixer; the warning is emitted in checking mode too.

diktat versions: 0.1.5

Pull request:
### What's done:
   Fixed bug, added tests
   closes #552

Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/KdocComments.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/KdocComments.kt
index baf830286c..7b10ba4cb1 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/KdocComments.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/KdocComments.kt
@@ -161,12 +161,14 @@ class KdocComments(private val configRules: List<RulesConfig>) : Rule("kdoc-comm
 
     @Suppress("UnsafeCallOnNullableType")
     private fun createKdocBasicKdoc(node: ASTNode) {
-        KDOC_NO_CONSTRUCTOR_PROPERTY.warnAndFix(configRules, emitWarn, isFixMode,
-                "add <${node.findChildByType(IDENTIFIER)!!.text}> to KDoc", node.startOffset, node) {
-            val newKdoc = KotlinParser().createNode("/**\n * @property ${node.findChildByType(IDENTIFIER)!!.text}\n */")
-            val classNode = node.parent({ it.elementType == CLASS })!!
-            classNode.addChild(PsiWhiteSpaceImpl("\n"), classNode.firstChildNode)
-            classNode.addChild(newKdoc.findChildByType(KDOC)!!, classNode.firstChildNode)
+        if (node.getFirstChildWithType(MODIFIER_LIST).isAccessibleOutside()) {
+            KDOC_NO_CONSTRUCTOR_PROPERTY.warnAndFix(configRules, emitWarn, isFixMode,
+                    "add <${node.findChildByType(IDENTIFIER)!!.text}> to KDoc", node.startOffset, node) {
+                val newKdoc = KotlinParser().createNode("/**\n * @property ${node.findChildByType(IDENTIFIER)!!.text}\n */")
+                val classNode = node.parent({ it.elementType == CLASS })!!
+                classNode.addChild(PsiWhiteSpaceImpl("\n"), classNode.firstChildNode)
+                classNode.addChild(newKdoc.findChildByType(KDOC)!!, classNode.firstChildNode)
+            }
         }
     }
 


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocCommentsWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocCommentsWarnTest.kt
index 315081e1c3..47745b69a8 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocCommentsWarnTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocCommentsWarnTest.kt
@@ -432,8 +432,7 @@ class KdocCommentsWarnTest : LintTestBase(::KdocComments) {
                     |}
                 """.trimMargin(),
                 LintError(1, 1, ruleId, "${MISSING_KDOC_TOP_LEVEL.warnText()} Example"),
-                LintError(2, 4, ruleId, "${KDOC_NO_CONSTRUCTOR_PROPERTY.warnText()} add <name> to KDoc", true),
-                LintError(3, 4, ruleId, "${KDOC_NO_CONSTRUCTOR_PROPERTY.warnText()} add <surname> to KDoc", true)
+                LintError(2, 4, ruleId, "${KDOC_NO_CONSTRUCTOR_PROPERTY.warnText()} add <name> to KDoc", true)
         )
     }
 }
diff --git a/diktat-rules/src/test/resources/test/paragraph2/kdoc/ConstructorCommentNoKDocExpected.kt b/diktat-rules/src/test/resources/test/paragraph2/kdoc/ConstructorCommentNoKDocExpected.kt
index b9e7799a4f..4743bc6509 100644
--- a/diktat-rules/src/test/resources/test/paragraph2/kdoc/ConstructorCommentNoKDocExpected.kt
+++ b/diktat-rules/src/test/resources/test/paragraph2/kdoc/ConstructorCommentNoKDocExpected.kt
@@ -22,9 +22,8 @@ class A constructor(
 ){}
 
 /**
- * @property name
-  * @property lastName
-*/
+ * @property lastName
+ */
 class A constructor(
         private var name: String,
         val lastName: String
@@ -37,3 +36,18 @@ class A constructor(
 class A constructor(
         val name: String
 ){}
+
+class Example(private val foo: Int)
+
+/**
+ * @property g
+  * @property e
+ * @property s
+*/
+class Example constructor(
+        val g: String,
+        private var q: String,
+        @param:JsonProperty("shortName") private val shortName: String,
+        val e: String,
+        protected val s: String
+){}
diff --git a/diktat-rules/src/test/resources/test/paragraph2/kdoc/ConstructorCommentNoKDocTest.kt b/diktat-rules/src/test/resources/test/paragraph2/kdoc/ConstructorCommentNoKDocTest.kt
index 09899c53c7..26e317b8bb 100644
--- a/diktat-rules/src/test/resources/test/paragraph2/kdoc/ConstructorCommentNoKDocTest.kt
+++ b/diktat-rules/src/test/resources/test/paragraph2/kdoc/ConstructorCommentNoKDocTest.kt
@@ -30,3 +30,13 @@ class A constructor(
          */
         val name: String
 ){}
+
+class Example(private val foo: Int)
+
+class Example constructor(
+        val g: String,
+        private var q: String,
+        @param:JsonProperty("shortName") private val shortName: String,
+        val e: String,
+        protected val s: String
+){}