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

Bugfix. EMPTY_BLOCK_STRUCTURE_ERROR shouldn't trigger on empty lambdas #803

Merged
merged 8 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,19 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

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

What is this underscore?

Suggested change
__# <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 = {})__
Copy link
Member

Choose a reason for hiding this comment

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

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

```

**Invalid examples:**
Expand Down