diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRule.kt index 05131d4eb6..3f5df33ee1 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRule.kt @@ -87,7 +87,7 @@ public class SpacingAroundCurlyRule : prevLeaf is PsiWhiteSpace || prevLeaf?.elementType == AT || ( - prevLeaf?.elementType == LPAR && + (prevLeaf?.elementType == LPAR || prevLeaf?.elementType == LBRACKET) && ((node as LeafPsiElement).parent is KtLambdaExpression || node.parent.parent is KtLambdaExpression) ) spacingAfter = nextLeaf is PsiWhiteSpace || nextLeaf?.elementType == RBRACE diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt index d668516bac..13a0d5fc09 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt @@ -179,6 +179,29 @@ class SpacingAroundCurlyRuleTest { .isFormattedAs(formattedCode) } + @Test + fun `Issue 2675 - array access expression containing a lambda expression`() { + val code = + """ + val foo1 = bar[{a -> a}] + val foo2 = bar[{ }] + val foo3 = bar[{}] + """.trimIndent() + + val formattedCode = + """ + val foo1 = bar[{ a -> a }] + val foo2 = bar[{ }] + val foo3 = bar[{}] + """.trimIndent() + + spacingAroundCurlyRuleAssertThat(code) + .hasLintViolations( + LintViolation(1, 17, "Missing spacing after \"{\"", canBeAutoCorrected = true), + LintViolation(1, 23, "Missing spacing before \"}\"", canBeAutoCorrected = true), + ).isFormattedAs(formattedCode) + } + @Test fun `Given curly braces inside a string template`() { val code = diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundSquareBracketsRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundSquareBracketsRuleTest.kt index 408354acbe..699bea6a84 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundSquareBracketsRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundSquareBracketsRuleTest.kt @@ -1,6 +1,7 @@ package com.pinterest.ktlint.ruleset.standard.rules import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule +import com.pinterest.ktlint.test.LintViolation import org.junit.jupiter.api.Test class SpacingAroundSquareBracketsRuleTest { @@ -125,4 +126,23 @@ class SpacingAroundSquareBracketsRuleTest { """.trimIndent() spacingAroundSquareBracketsRuleAssertThat(code).hasNoLintViolations() } + + @Test + fun `Issue 2675 - array access expression containing a lambda expression`() { + val code = + """ + val foo = bar[ { 123 } ] + """.trimIndent() + + val formattedCode = + """ + val foo = bar[{ 123 }] + """.trimIndent() + + spacingAroundSquareBracketsRuleAssertThat(code) + .hasLintViolations( + LintViolation(1, 15, "Unexpected spacing after '['", canBeAutoCorrected = true), + LintViolation(1, 23, "Unexpected spacing before ']'", canBeAutoCorrected = true), + ).isFormattedAs(formattedCode) + } }