Skip to content

Commit

Permalink
Fixed false positive MISSING_KDOC_ON_FUNCTION on local function (fu…
Browse files Browse the repository at this point in the history
…nction inside another function) (#1848)

### What's done:
- Fixed false positive `MISSING_KDOC_ON_FUNCTION` on local function (function inside another function)
- Added warning tests

Closes #1773
  • Loading branch information
diphtongue authored Dec 7, 2023
1 parent 5511527 commit 09edb26
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.KtNodeTypes.DESTRUCTURING_DECLARATION
import org.jetbrains.kotlin.KtNodeTypes.DESTRUCTURING_DECLARATION_ENTRY
import org.jetbrains.kotlin.KtNodeTypes.FUNCTION_TYPE
import org.jetbrains.kotlin.KtNodeTypes.OBJECT_DECLARATION
import org.jetbrains.kotlin.KtNodeTypes.PROPERTY
import org.jetbrains.kotlin.KtNodeTypes.REFERENCE_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.TYPE_PARAMETER
import org.jetbrains.kotlin.KtNodeTypes.TYPE_REFERENCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,22 @@ class KdocMethods(configRules: List<RulesConfig>) : DiktatRule(
}
}

@Suppress("UnsafeCallOnNullableType", "AVOID_NULL_CHECKS")
private fun hasFunParent(node: ASTNode): Boolean {
var parent = node.treeParent
while (parent != null) {
if (parent.elementType == FUN) {
return true
}
parent = parent.treeParent
}
return false
}

@Suppress(
"UnsafeCallOnNullableType",
"AVOID_NULL_CHECKS",
"CyclomaticComplexMethod"
)
private fun checkSignatureDescription(node: ASTNode) {
val kdoc = node.getFirstChildWithType(KDOC)
val kdocTags = kdoc?.kDocTags()
Expand All @@ -119,7 +134,9 @@ class KdocMethods(configRules: List<RulesConfig>) : DiktatRule(

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) {
if (kdoc == null && hasFunParent(node)) {
return
} else if (kdoc == null && anyTagFailed) {
addKdocTemplate(node, name, missingParameters, explicitlyThrownExceptions, returnCheckFailed)
} else if (kdoc == null && !isReferenceExpressionWithSameName(node)) {
MISSING_KDOC_ON_FUNCTION.warn(configRules, emitWarn, name, node.startOffset, node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,103 @@ class KdocMethodsTest : LintTestBase(::KdocMethods) {
DiktatError(2, 1, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} writeToConsoleEx", true),
)
}

@Test
@Tag(WarningNames.MISSING_KDOC_ON_FUNCTION)
fun `KDoc shouldn't trigger on local functions`() {
lintMethod(
"""
|fun printHelloAndBye() {
| fun printHello() {
| print("Hello")
| }
| printHello()
| val ab = 5
| ab?.let {
| fun printBye() {
| print("Bye")
| }
| printBye()
| }
|}
""".trimMargin(),
DiktatError(1, 1, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} printHelloAndBye", false),
)
}

@Test
@Tag(WarningNames.MISSING_KDOC_ON_FUNCTION)
fun `KDoc shouldn't trigger on functions with KDoc`() {
lintMethod(
"""
|/**
| * prints "Hello" and "Bye"
| */
|fun printHelloAndBye() {
| fun printHello() {
| print("Hello")
| }
| printHello()
| val ab = 5
| ab?.let {
| fun printBye() {
| print("Bye")
| }
| printBye()
| }
|}
""".trimMargin(),
)
}

@Test
@Tag(WarningNames.MISSING_KDOC_ON_FUNCTION)
fun `KDoc shouldn't trigger on nested local functions`() {
lintMethod(
"""
|fun printHelloAndBye() {
| fun printHello() {
| print("Hello")
| fun printBye() {
| print("Bye")
| }
| fun printDots() {
| print("...")
| }
| printBye()
| printDots()
| }
| printHello()
|}
""".trimMargin(),
DiktatError(1, 1, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} printHelloAndBye", false),
)
}

@Test
@Tag(WarningNames.MISSING_KDOC_ON_FUNCTION)
fun `KDoc shouldn't trigger on local functions with KDoc`() {
lintMethod(
"""
|fun printHelloAndBye() {
| fun printHello() {
| print("Hello")
| }
| printHello()
| val ab = 5
| ab?.let {
| /**
| * prints "Bye"
| */
| fun printBye() {
| print("Bye")
| }
| printBye()
| }
|}
""".trimMargin(),
DiktatError(1, 1, ruleId, "${MISSING_KDOC_ON_FUNCTION.warnText()} printHelloAndBye", false),
)
}

}

0 comments on commit 09edb26

Please sign in to comment.