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 bceb7be commit cda1e91
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.LPAR
import com.pinterest.ktlint.core.ast.ElementType.RBRACE
import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT
import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT_LIST
import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
Expand Down Expand Up @@ -106,8 +108,9 @@ class EmptyBlock(configRules: List<RulesConfig>) : DiktatRule(
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
// excepting cases like list.map { }. In this case call expression will not have value argument list
// And in this case: Parser.parse({}, some, thing) it will have value argument list
callExpression.hasChildOfType(VALUE_ARGUMENT_LIST)
}
parents.any { it.elementType == LAMBDA_EXPRESSION } -> {
val lambdaExpression = parents.find { it.elementType == LAMBDA_EXPRESSION }!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class KotlinParser {
}
} // I don't really understand what's going on here, but thanks to this, you can use this node in the future
val project = KotlinCoreEnvironment.createForProduction(
Disposable { },
{ },
compilerConfiguration,
EnvironmentConfigFiles.JVM_CONFIG_FILES
).project // create project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import org.cqfn.diktat.util.LintTestBase

import com.pinterest.ktlint.core.LintError
import generated.WarningNames
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test

Expand Down Expand Up @@ -221,4 +223,21 @@ class EmptyBlockWarnTest : LintTestBase(::EmptyBlock) {
rulesConfigList = rulesConfigListEmptyBlockExist
)
}

@Test
@Tag(WarningNames.EMPTY_BLOCK_STRUCTURE_ERROR)
fun `should not trigger on empty lambdas as a functions #2`() {
lintMethod(
"""
|fun some() {
| val project = KotlinCoreEnvironment.createForProduction(
| { },
| compilerConfiguration,
| EnvironmentConfigFiles.JVM_CONFIG_FILES
| ).project
|}
""".trimMargin(),
rulesConfigList = rulesConfigListEmptyBlockExist
)
}
}

0 comments on commit cda1e91

Please sign in to comment.