Repository: cqfn/diKTat (kotlin)

Issue:
NPE: Internal error has occurred in newlines
## Describe the bug
On this code:
```kotlin
fun spread(jsObject: Any, handler: (key: String, value: Any) -> Unit) {
}
```
diktat crashes with the following exception:
```
[ERROR] 2021-03-03 15:55:52 Internal error has occurred in newlines. Please make an issue on this bug at https://github.com/cqfn/diKTat/.
java.lang.NullPointerException: it must not be null
	at org.cqfn.diktat.ruleset.rules.chapter3.files.NewlinesRule$handleFirstValue$$inlined$let$lambda$1.invoke(NewlinesRule.kt:360)
	at org.cqfn.diktat.ruleset.rules.chapter3.files.NewlinesRule$handleFirstValue$$inlined$let$lambda$1.invoke(NewlinesRule.kt:100)
```

## Environment information
* diktat version: 0.4.1

Pull request:
### What's done:
* Fixed NPE in NewLineRule for lambda list argument
closes #789

Patch:
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<RulesConfig>) : 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<RulesConfig>) : 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()
+                    )
+                }
             }
         }
 


Test patch:
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