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

NullPointerException on KDOC inspection #1334

Merged
merged 13 commits into from
Jun 2, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ class IdentifierNaming(configRules: List<RulesConfig>) : DiktatRule(
* 4) backticks are prohibited in the naming of non-test methods
*/
@Suppress("UnsafeCallOnNullableType")
private fun checkFunctionName(node: ASTNode): List<ASTNode> {
val functionName = node.getIdentifierName()!!
private fun checkFunctionName(node: ASTNode): List<ASTNode>? {
val functionName = node.getIdentifierName() ?: return null

// basic check for camel case
if (!functionName.text.isLowerCamelCase()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.cqfn.diktat.ruleset.utils.hasKnownKdocTag
import org.cqfn.diktat.ruleset.utils.hasTestAnnotation
import org.cqfn.diktat.ruleset.utils.insertTagBefore
import org.cqfn.diktat.ruleset.utils.isAccessibleOutside
import org.cqfn.diktat.ruleset.utils.isAnonymousFunction
import org.cqfn.diktat.ruleset.utils.isGetterOrSetter
import org.cqfn.diktat.ruleset.utils.isLocatedInTest
import org.cqfn.diktat.ruleset.utils.isOverridden
Expand Down Expand Up @@ -88,7 +89,7 @@ class KdocMethods(configRules: List<RulesConfig>) : DiktatRule(
val config = configRules.getCommonConfiguration()
val filePath = node.getFilePath()
val isTestMethod = node.hasTestAnnotation() || isLocatedInTest(filePath.splitPathToDirs(), config.testAnchors)
if (!isTestMethod && !node.isStandardMethod() && !node.isSingleLineGetterOrSetter()) {
if (!isTestMethod && !node.isStandardMethod() && !node.isSingleLineGetterOrSetter() && !node.isAnonymousFunction()) {
checkSignatureDescription(node)
}
} else if (node.elementType == KDOC_SECTION) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.cqfn.diktat.ruleset.utils.findAllDescendantsWithSpecificType
import org.cqfn.diktat.ruleset.utils.getFirstChildWithType
import org.cqfn.diktat.ruleset.utils.hasChildOfType
import org.cqfn.diktat.ruleset.utils.hasParent
import org.cqfn.diktat.ruleset.utils.isAnonymousFunction

import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY
import com.pinterest.ktlint.core.ast.ElementType.FUN
Expand Down Expand Up @@ -65,7 +66,8 @@ class AvoidNestedFunctionsRule(configRules: List<RulesConfig>) : DiktatRule(
}

private fun isNestedFunction(node: ASTNode): Boolean =
node.hasParent(FUN) && node.hasFunParentUntil(CLASS_BODY) && !node.hasChildOfType(MODIFIER_LIST)
node.hasParent(FUN) && node.hasFunParentUntil(CLASS_BODY) && !node.hasChildOfType(MODIFIER_LIST) &&
!node.isAnonymousFunction()

private fun ASTNode.hasFunParentUntil(stopNode: IElementType): Boolean =
parents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ fun ASTNode.replaceWhiteSpaceText(beforeNode: ASTNode, text: String) {
fun ASTNode.getFirstChildWithType(elementType: IElementType): ASTNode? =
this.findChildByType(elementType)

/**
* Checks if a function is anonymous
*/
fun ASTNode.isAnonymousFunction() = this.getIdentifierName() == null
petertrr marked this conversation as resolved.
Show resolved Hide resolved

/**
* Checks if the symbols in this node are at the end of line
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
fixAndCompareSmokeTest("DefaultPackageExpected.kt", "DefaultPackageTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #8 - anonymous function`() {
fixAndCompareSmokeTest("Example8Expected.kt", "Example8Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #7`() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.cqfn.diktat

fun foo() {
val sum: (Int, Int, Int,) -> Int = fun(
Copy link
Member

@orchestr7 orchestr7 Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also add a unit test (at list for checks), even duplicated. Not only smoke is needed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

x,
y,
z
): Int = x + y + x
println(sum(8, 8, 8))
}

fun boo() {
val message = fun() = println("Hello")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.cqfn.diktat

fun foo() {
val sum: (Int, Int, Int,) -> Int = fun(
x,
y,
z
): Int {
return x + y + x
}
println(sum(8, 8, 8))
}

fun boo() {
val message = fun()=println("Hello")
}