Repository: analysis-dev/diktat (kotlin)

Issue:
Lambda passed as an argument is forced to be on the same line as the previous argument
## Describe the bug
```kotlin
fun foo() {
    foo(
        0,
        { obj -> obj.bar() }
    )
}
```
=>
```kotlin
fun foo() {
    foo(
            0, { obj -> obj.bar() }
    )
}
```

## Environment information
* diktat version: 0.1.3

Pull request description:
### What's done:
  * Fixed bugs


This pull request closes #490

Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/WhiteSpaceRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/WhiteSpaceRule.kt
index 1e5cfe1085..006f35004f 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/WhiteSpaceRule.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/WhiteSpaceRule.kt
@@ -174,7 +174,7 @@ class WhiteSpaceRule(private val configRules: List<RulesConfig>) : Rule("horizon
     /**
      * This method covers all other opening braces, not covered in [handleKeywordWithParOrBrace].
      */
-    @Suppress("UnsafeCallOnNullableType")
+    @Suppress("UnsafeCallOnNullableType", "ComplexMethod")
     private fun handleLbrace(node: ASTNode) {
         // `{` can't be the very first symbol in the file, so `!!` should be safe
         val whitespaceOrPrevNode = node.selfOrParentsTreePrev()!!
@@ -188,16 +188,20 @@ class WhiteSpaceRule(private val configRules: List<RulesConfig>) : Rule("horizon
                             it[1].elementType == LAMBDA_EXPRESSION &&
                             it[2].elementType == VALUE_ARGUMENT &&
                             // lambda is not passed as a named argument
-                            !it[2].hasChildOfType(EQ) &&
-                            // lambda is the first argument in the list
-                            it[2].prevSibling { prevNode -> prevNode.elementType == COMMA } == null
+                            !it[2].hasChildOfType(EQ)
                 }
                 ?: false
+
         val prevNode = whitespaceOrPrevNode.let { if (it.elementType == WHITE_SPACE) it.treePrev else it }
         val numWhiteSpace = whitespaceOrPrevNode.numWhiteSpaces()
         // note: the conditions in the following `if`s cannot be collapsed into simple conjunctions
         if (isFromLambdaAsArgument) {
-            if (numWhiteSpace != 0) {
+            val isFirstArgument = node
+                    .parent({ it.elementType == VALUE_ARGUMENT })
+                    .let { it?.prevSibling { prevNode -> prevNode.elementType == COMMA } == null }
+
+            // If it is lambda, then we don't force it to be on newline or same line
+            if (numWhiteSpace != 0 && isFirstArgument) {
                 WRONG_WHITESPACE.warnAndFix(configRules, emitWarn, isFixMode, "there should be no whitespace before '{' of lambda" +
                         " inside argument list", node.startOffset, node) {
                     whitespaceOrPrevNode.treeParent.removeChild(whitespaceOrPrevNode)


Test patch:
diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example1Test.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example1Test.kt
index 339c6e136b..324c351a54 100644
--- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example1Test.kt
+++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example1Test.kt
@@ -27,4 +27,5 @@ return   x+ y}
      bar(x,y)
   )
     }
-}
\ No newline at end of file
+}
+
diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt
index 88d0e2e229..dfd13fe03d 100644
--- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt
+++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Expected.kt
@@ -32,3 +32,14 @@ fun `method name incorrect, part 4`() {
             .foo()
 }
 
+fun foo() {
+    foo(
+            0,
+            { obj -> obj.bar() }
+    )
+
+    bar(
+            0, { obj -> obj.bar() }
+    )
+}
+
diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt
index 0b4819e80b..6a66c874c0 100644
--- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt
+++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example4Test.kt
@@ -31,3 +31,14 @@ fun `method name incorrect, part 4`() {
             .foo()
 }
 
+fun foo() {
+    foo(
+            0,
+            { obj -> obj.bar() }
+    )
+
+    bar(
+            0, { obj -> obj.bar() }
+    )
+}
+