Skip to content

Commit

Permalink
bugfix/empty-lambdas(#796)
Browse files Browse the repository at this point in the history
### What's done:
  * Fixed bug
  • Loading branch information
aktsay6 committed Mar 19, 2021
1 parent cff24e6 commit 430b0bd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ class EmptyBlock(configRules: List<RulesConfig>) : DiktatRule(
val configuration = EmptyBlockStyleConfiguration(
configRules.getRuleConfig(EMPTY_BLOCK_STRUCTURE_ERROR)?.configuration ?: emptyMap()
)
if (node.elementType == FILE) {
println(node.prettyPrint())
}
searchNode(node, configuration)
}

Expand Down Expand Up @@ -106,16 +103,21 @@ class EmptyBlock(configRules: List<RulesConfig>) : DiktatRule(
@Suppress("UnsafeCallOnNullableType")
private fun isLambdaUsedAsFunction(node: ASTNode): Boolean {
val parents = node.parents()
if (parents.any { it.elementType == CALL_EXPRESSION }) {
val callExpression = parents.find { it.elementType == CALL_EXPRESSION }!!
// excepting cases like list.map { }
return callExpression.treeParent.elementType != DOT_QUALIFIED_EXPRESSION
} else if (parents.any { it.elementType == LAMBDA_EXPRESSION }) {
val lambdaExpression = parents.find { it.elementType == LAMBDA_EXPRESSION }!!
// cases like A({}). Here Lambda expression is used as a value parameter.
return lambdaExpression.treeParent.elementType == VALUE_PARAMETER
return when {
parents.any { it.elementType == CALL_EXPRESSION } -> {
val callExpression = parents.find { it.elementType == CALL_EXPRESSION }!!
// excepting cases like list.map { }
callExpression.treeParent.elementType != DOT_QUALIFIED_EXPRESSION
}
parents.any { it.elementType == LAMBDA_EXPRESSION } -> {
val lambdaExpression = parents.find { it.elementType == LAMBDA_EXPRESSION }!!
// cases like A({}). Here Lambda expression is used as a value parameter.
lambdaExpression.treeParent.elementType == VALUE_PARAMETER
}
else -> {
false
}
}
return false
}

/**
Expand Down
6 changes: 4 additions & 2 deletions info/guide/guide-chapter-3.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# <a name="c3"></a>3. General formatting (typesetting)
__# <a name="c3"></a>3. General formatting (typesetting)
<!-- =============================================================================== -->
### <a name="c3.1"></a> 3.1 File-related rules
This section describes the rules related to using files in your code.
Expand Down Expand Up @@ -305,7 +305,7 @@ class A
Avoid empty blocks, and ensure braces start on a new line. An empty code block can be closed immediately on the same line and the next line. However, a newline is recommended between opening and closing braces `{}` (see the examples below.)

Generally, empty code blocks are prohibited; using them is considered a bad practice (especially for catch block).
They are only appropriate for overridden functions when the base class's functionality is not needed in the class-inheritor.
They are appropriate for overridden functions, when the base class's functionality is not needed in the class-inheritor, for lambdas used as a function and for empty function in implementation of functional interface.
```kotlin
override fun foo() {
}
Expand All @@ -318,6 +318,8 @@ fun doNothing() {}

fun doNothingElse() {
}

fun foo(bar: () -> Unit = {})__
```

**Invalid examples:**
Expand Down

0 comments on commit 430b0bd

Please sign in to comment.