Repository: analysis-dev/diktat (kotlin)

Issue:
Warnings on individual KDoc tags shouldn't be triggered if KDoc is not present

Pull request:
### What's done:
    Fixed rule and tests, added tests
closes #577

Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/KdocMethods.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/KdocMethods.kt
index 6f1d247a93..3eb84eda7b 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/KdocMethods.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/kdoc/KdocMethods.kt
@@ -113,22 +113,22 @@ class KdocMethods(private val configRules: List<RulesConfig>) : Rule("kdoc-metho
         val returnCheckFailed = hasReturnCheckFailed(node, kdocTags)
         val throwsCheckFailed = missingExceptions.isNotEmpty()
 
-        if (paramCheckFailed) {
-            handleParamCheck(node, kdoc, missingParameters, kDocMissingParameters, kdocTags)
-        }
-        if (returnCheckFailed) {
-            handleReturnCheck(node, kdoc, kdocTags)
-        }
-        if (throwsCheckFailed) {
-            handleThrowsCheck(node, kdoc, missingExceptions)
-        }
-
-        // if no tag failed, we have too little information to suggest KDoc - it would just be empty
         val anyTagFailed = paramCheckFailed || returnCheckFailed || throwsCheckFailed
+        // if no tag failed, we have too little information to suggest KDoc - it would just be empty
         if (kdoc == null && anyTagFailed) {
             addKdocTemplate(node, name, missingParameters, explicitlyThrownExceptions, returnCheckFailed)
         } else if (kdoc == null) {
             MISSING_KDOC_ON_FUNCTION.warn(configRules, emitWarn, false, name, node.startOffset, node)
+        } else {
+            if (paramCheckFailed) {
+                handleParamCheck(node, kdoc, missingParameters, kDocMissingParameters, kdocTags)
+            }
+            if (returnCheckFailed) {
+                handleReturnCheck(node, kdoc, kdocTags)
+            }
+            if (throwsCheckFailed) {
+                handleThrowsCheck(node, kdoc, missingExceptions)
+            }
         }
     }
 


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocMethodsTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocMethodsTest.kt
index 009cad3e52..3c8a123e3d 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocMethodsTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocMethodsTest.kt
@@ -61,17 +61,11 @@ class KdocMethodsTest : LintTestBase(::KdocMethods) {
         lintMethod(validCode, fileName = "src/main/kotlin/org/cqfn/diktat/Example.kt")
         // no false positive triggers on annotations
         lintMethod(complexAnnotationCode,
-            LintError(1, 1, ruleId, "${KDOC_WITHOUT_PARAM_TAG.warnText()} doubleInt (a)", true),
-            LintError(1, 1, ruleId, "${KDOC_WITHOUT_RETURN_TAG.warnText()} doubleInt", true),
-            LintError(1, 1, ruleId, "${KDOC_WITHOUT_THROWS_TAG.warnText()} doubleInt (IllegalStateException)", true),
             LintError(1, 1, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} doubleInt", true),
             fileName = "src/main/kotlin/org/cqfn/diktat/Example.kt"
         )
         // should check all .kt files unless both conditions on location and name are true
         lintMethod(funCode,
-            LintError(1, 1, ruleId, "${KDOC_WITHOUT_PARAM_TAG.warnText()} doubleInt (a)", true),
-            LintError(1, 1, ruleId, "${KDOC_WITHOUT_RETURN_TAG.warnText()} doubleInt", true),
-            LintError(1, 1, ruleId, "${KDOC_WITHOUT_THROWS_TAG.warnText()} doubleInt (IllegalStateException)", true),
             LintError(1, 1, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} doubleInt", true),
             fileName = "src/test/kotlin/org/cqfn/diktat/Example.kt"
         )
@@ -308,9 +302,7 @@ class KdocMethodsTest : LintTestBase(::KdocMethods) {
                     |}
                 """.trimMargin(),
             LintError(10, 5, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} getY", false),
-            LintError(12, 5, ruleId, "${KDOC_WITHOUT_PARAM_TAG.warnText()} setY (y)", true),
             LintError(12, 5, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} setY", true),
-            LintError(17, 5, ruleId, "${KDOC_WITHOUT_RETURN_TAG.warnText()} getZ", true),
             LintError(17, 5, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} getZ", true)
         )
     }
@@ -353,4 +345,19 @@ class KdocMethodsTest : LintTestBase(::KdocMethods) {
                 """.trimMargin()
         )
     }
+
+    @Test
+    @Tag(WarningNames.MISSING_KDOC_ON_FUNCTION)
+    fun `should check if KfDoc is not trivial`() {
+        lintMethod(
+            """
+                    |fun foo(x: Int): TypeX { 
+                    |   val q = goo()
+                    |   throw UnsupportedOperationException()
+                    |   return qwe 
+                    |}
+                """.trimMargin(),
+            LintError(1, 1, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", true)
+        )
+    }
 }
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocParamPresentWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocParamPresentWarnTest.kt
index 0aead5a7b3..95d6c374ef 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocParamPresentWarnTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/KdocParamPresentWarnTest.kt
@@ -103,7 +103,6 @@ class KdocParamPresentWarnTest : LintTestBase(::KdocMethods) {
                     |* @param a - qwe
                     |*/
                 """.trimMargin(),
-            LintError(1, 1, ruleId, "${KDOC_WITHOUT_PARAM_TAG.warnText()} foo (b, a)", true),
             LintError(1, 1, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} foo", true)
         )
     }