Repository: analysis-dev/diktat (kotlin)

Issue:
Make KDocs on overriden functions/properties not mandatory
Or even false by default, because dokka uses parent class documentation when generating docs.

Pull request:
### What's done:
  * Fixed bugs

This pull request closes #551

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 53a5615673..baf830286c 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
@@ -41,11 +41,13 @@ import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_NO_CONSTRUCTOR_PROPERTY
 import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_NO_CONSTRUCTOR_PROPERTY_WITH_COMMENT
 import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_KDOC_TOP_LEVEL
 import org.cqfn.diktat.ruleset.constants.Warnings.MISSING_KDOC_CLASS_ELEMENTS
+import org.cqfn.diktat.ruleset.utils.isOverridden
 import org.jetbrains.kotlin.com.intellij.lang.ASTNode
 import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
 import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
 import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet
 import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag
+import org.jetbrains.kotlin.psi.KtNamedFunction
 import org.jetbrains.kotlin.psi.psiUtil.parents
 
 /**
@@ -259,7 +261,10 @@ class KdocComments(private val configRules: List<RulesConfig>) : Rule("kdoc-comm
         if (classBody != null && modifier.isAccessibleOutside()) {
             classBody
                     .getChildren(statementsToDocument)
-                    .filterNot { it.elementType == FUN && it.isStandardMethod() }
+                    .filterNot { (it.elementType == FUN && it.isStandardMethod())
+                            || (it.elementType == FUN && it.isOverridden())
+                            || (it.elementType == PROPERTY && it.isOverridden())
+                    }
                     .forEach { checkDoc(it, MISSING_KDOC_CLASS_ELEMENTS) }
         }
     }
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FunctonASTNodeUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FunctonASTNodeUtils.kt
index 0fe4cd741b..562a31638d 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FunctonASTNodeUtils.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/FunctonASTNodeUtils.kt
@@ -6,6 +6,7 @@ import com.pinterest.ktlint.core.ast.ElementType.BLOCK
 import com.pinterest.ktlint.core.ast.ElementType.CONSTRUCTOR_CALLEE
 import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
 import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST
+import com.pinterest.ktlint.core.ast.ElementType.OVERRIDE_KEYWORD
 import org.cqfn.diktat.ruleset.rules.PackageNaming.Companion.PACKAGE_PATH_ANCHOR
 import org.jetbrains.kotlin.com.intellij.lang.ASTNode
 import org.jetbrains.kotlin.psi.KtFunction
@@ -57,6 +58,12 @@ fun ASTNode.isStandardMethod() = also(::checkNodeIsFun)
         ?.let { it.text in STANDARD_METHODS }
         ?: false
 
+
+fun ASTNode.isOverridden() : Boolean =
+        getFirstChildWithType(MODIFIER_LIST)
+        ?.hasChildOfType(OVERRIDE_KEYWORD) ?: false
+
+
 private fun ASTNode.argList(): ASTNode? {
     checkNodeIsFun(this)
     return this.getFirstChildWithType(ElementType.VALUE_PARAMETER_LIST)


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocCommentsWarnTest.kt
similarity index 90%
rename from diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocWarnTest.kt
rename to diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocCommentsWarnTest.kt
index a8d18e0799..315081e1c3 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocWarnTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocCommentsWarnTest.kt
@@ -10,7 +10,7 @@ import org.cqfn.diktat.util.LintTestBase
 import org.junit.jupiter.api.Tag
 import org.junit.jupiter.api.Tags
 
-class KdocWarnTest : LintTestBase(::KdocComments) {
+class KdocCommentsWarnTest : LintTestBase(::KdocComments) {
     private val ruleId: String = "$DIKTAT_RULE_SET_ID:kdoc-comments"
 
     @Test
@@ -129,6 +129,40 @@ class KdocWarnTest : LintTestBase(::KdocComments) {
         )
     }
 
+    @Test
+    @Tag(WarningNames.MISSING_KDOC_CLASS_ELEMENTS)
+    fun `Kdoc shouldn't not be mandatory for overridden functions and props`() {
+        val code =
+                """
+                    /**
+                    * class that contains fields, functions and public subclasses
+                    **/
+                    class SomeGoodName : Another {
+                        val variable: String = ""
+                        private val privateVariable: String = ""
+                        override val someVal: String = ""
+                        fun perfectFunction() {
+                        }
+
+                        override fun overrideFunction() {
+                        }
+
+                        class InternalClass {
+                        }
+
+                        private class InternalClass {
+                        }
+                        
+                        public fun main() {}
+                    }
+                """.trimIndent()
+        lintMethod(code,
+                LintError(5, 5, ruleId, "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} variable"),
+                LintError(8, 5, ruleId, "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} perfectFunction"),
+                LintError(14, 5, ruleId, "${MISSING_KDOC_CLASS_ELEMENTS.warnText()} InternalClass")
+        )
+    }
+
     @Test
     @Tag(WarningNames.MISSING_KDOC_CLASS_ELEMENTS)
     fun `Kdoc shouldn't present for each class element because Test annotation`() {