Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed false positive MISSING_KDOC_ON_FUNCTION on local function (function inside another function) #1848

Merged
merged 6 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
diphtongue marked this conversation as resolved.
Show resolved Hide resolved
| 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),
)
}

}
Loading