From 386ae915ee37bab4141d24b94ea6f338a2a7d7a1 Mon Sep 17 00:00:00 2001 From: Vladislav Frolov <50615459+Cheshiriks@users.noreply.github.com> Date: Tue, 9 Mar 2021 14:33:42 +0300 Subject: [PATCH] Fix NPE in NewLineRule (#792) ### What's done: * Fixed NPE in NewLineRule for lambda list argument --- .../rules/chapter3/files/NewlinesRule.kt | 25 +++++++++++++------ .../chapter3/files/NewlinesRuleFixTest.kt | 6 +++++ .../newlines/ListArgumentLambdaExpected.kt | 15 +++++++++++ .../newlines/ListArgumentLambdaTest.kt | 15 +++++++++++ 4 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 diktat-rules/src/test/resources/test/paragraph3/newlines/ListArgumentLambdaExpected.kt create mode 100644 diktat-rules/src/test/resources/test/paragraph3/newlines/ListArgumentLambdaTest.kt diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt index 5f4fe3c62e..c005759454 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt @@ -12,6 +12,7 @@ import org.cqfn.diktat.ruleset.utils.appendNewlineMergingWhiteSpace import org.cqfn.diktat.ruleset.utils.emptyBlockList import org.cqfn.diktat.ruleset.utils.extractLineOfText import org.cqfn.diktat.ruleset.utils.findAllDescendantsWithSpecificType +import org.cqfn.diktat.ruleset.utils.findParentNodeWithSpecificType import org.cqfn.diktat.ruleset.utils.getIdentifierName import org.cqfn.diktat.ruleset.utils.hasParent import org.cqfn.diktat.ruleset.utils.isBeginByNewline @@ -352,9 +353,15 @@ class NewlinesRule(configRules: List) : DiktatRule( */ @Suppress("ComplexMethod") private fun handleList(node: ASTNode) { - if (node.elementType == VALUE_PARAMETER_LIST && node.treeParent.elementType.let { it == FUNCTION_TYPE || it == FUNCTION_TYPE_RECEIVER }) { + if (node.elementType == VALUE_PARAMETER_LIST) { + // do not check list parameter in lambda + node.findParentNodeWithSpecificType(LAMBDA_ARGUMENT)?.let { + return + } // do not check other value lists - return + if (node.treeParent.elementType.let { it == FUNCTION_TYPE || it == FUNCTION_TYPE_RECEIVER }) { + return + } } if (node.elementType == VALUE_ARGUMENT_LIST && node.siblings(forward = false).any { it.elementType == REFERENCE_EXPRESSION }) { @@ -400,12 +407,14 @@ class NewlinesRule(configRules: List) : DiktatRule( warnText } WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, freeText, node.startOffset, node) { - node.appendNewlineMergingWhiteSpace( - list.first() - .treePrev - .takeIf { it.elementType == WHITE_SPACE }, - list.first() - ) + list.first().treePrev?.let { + node.appendNewlineMergingWhiteSpace( + list.first() + .treePrev + .takeIf { it.elementType == WHITE_SPACE }, + list.first() + ) + } } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/NewlinesRuleFixTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/NewlinesRuleFixTest.kt index 12372c7ee8..8af3ecad11 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/NewlinesRuleFixTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/NewlinesRuleFixTest.kt @@ -80,4 +80,10 @@ class NewlinesRuleFixTest : FixTestBase("test/paragraph3/newlines", ::NewlinesRu fun `should fix one line function with and without semicolon`() { fixAndCompare("OneLineFunctionExpected.kt", "OneLineFunctionTest.kt") } + + @Test + @Tag(WarningNames.WRONG_NEWLINES) + fun `list argument in lambda`() { + fixAndCompare("ListArgumentLambdaExpected.kt", "ListArgumentLambdaTest.kt") + } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/newlines/ListArgumentLambdaExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/newlines/ListArgumentLambdaExpected.kt new file mode 100644 index 0000000000..ca09bcd93f --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/newlines/ListArgumentLambdaExpected.kt @@ -0,0 +1,15 @@ +package test.paragraph3.newlines + +class Example { + + fun foo() { + foldIndexed("#") { index: Int, acc: String, pathPart: String -> + } + } + + fun bar() { + foldIndexed("#") { index, acc, pathPart -> + } + } + +} \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/paragraph3/newlines/ListArgumentLambdaTest.kt b/diktat-rules/src/test/resources/test/paragraph3/newlines/ListArgumentLambdaTest.kt new file mode 100644 index 0000000000..ca09bcd93f --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/newlines/ListArgumentLambdaTest.kt @@ -0,0 +1,15 @@ +package test.paragraph3.newlines + +class Example { + + fun foo() { + foldIndexed("#") { index: Int, acc: String, pathPart: String -> + } + } + + fun bar() { + foldIndexed("#") { index, acc, pathPart -> + } + } + +} \ No newline at end of file