Repository: detekt/detekt (kotlin)

Issue:
OptionalWhenBraces false negative
Copied from https://github.com/detekt/detekt/issues/4245#issuecomment-1098366683

Issue is present in 1.19 and 1.20

```
import android.icu.text.PluralRules
import kotlinx.serialization.json.JsonObject
import java.util.Locale

fun JsonObject.plural(
  plural: Number?,
  language: String = "en"
) = when(plural) {
  null -> get("other")

  else -> {
    when {
      "other" in this -> get(
        PluralRules.forLocale(
          Locale.forLanguageTag(language)
        ).toString()
      )

      // other must be present even if a present rule would match
      else -> null
    }
  }
}
```

Pull request description:
Fixes #4715

Patch:
diff --git a/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalWhenBraces.kt b/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalWhenBraces.kt
index f1c545614c7f..0acd5ba1f4b3 100644
--- a/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalWhenBraces.kt
+++ b/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalWhenBraces.kt
@@ -7,10 +7,12 @@ import io.gitlab.arturbosch.detekt.api.Entity
 import io.gitlab.arturbosch.detekt.api.Issue
 import io.gitlab.arturbosch.detekt.api.Rule
 import io.gitlab.arturbosch.detekt.api.Severity
-import io.gitlab.arturbosch.detekt.rules.hasCommentInside
+import org.jetbrains.kotlin.com.intellij.psi.PsiComment
 import org.jetbrains.kotlin.psi.KtBlockExpression
+import org.jetbrains.kotlin.psi.KtExpression
 import org.jetbrains.kotlin.psi.KtLambdaExpression
 import org.jetbrains.kotlin.psi.KtWhenExpression
+import org.jetbrains.kotlin.psi.psiUtil.allChildren
 
 /**
  * This rule reports unnecessary braces in when expressions. These optional braces should be removed.
@@ -50,10 +52,11 @@ class OptionalWhenBraces(config: Config = Config.empty) : Rule(config) {
         super.visitWhenExpression(expression)
     }
 
-    private fun KtBlockExpression.hasUnnecessaryBraces(): Boolean {
-        val singleStatement = statements.singleOrNull()?.takeIf {
-            !(it is KtLambdaExpression && it.functionLiteral.arrow == null)
-        }
-        return singleStatement != null && !hasCommentInside() && lBrace != null && rBrace != null
-    }
+    private fun KtBlockExpression.hasUnnecessaryBraces(): Boolean =
+        lBrace != null && rBrace != null &&
+            statements.singleOrNull()?.takeIf { !it.isLambdaExpressionWithoutArrow() } != null &&
+            allChildren.none { it is PsiComment }
+
+    private fun KtExpression.isLambdaExpressionWithoutArrow(): Boolean =
+        this is KtLambdaExpression && this.functionLiteral.arrow == null
 }


Test patch:
diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalWhenBracesSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalWhenBracesSpec.kt
index 40faac0c15de..ab7d7bff909a 100644
--- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalWhenBracesSpec.kt
+++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalWhenBracesSpec.kt
@@ -72,6 +72,27 @@ class OptionalWhenBracesSpec {
                 .hasSourceLocations(SourceLocation(7, 17), SourceLocation(10, 17))
         }
 
+        @Test
+        fun `reports unnecessary braces when the single statement has comments inside`() {
+            val code = """
+                fun test(i: Int) {
+                    when {
+                        else -> {
+                            when (i) {
+                                // foo
+                                1 -> println(1)
+                                // bar
+                                else -> println(2)
+                            }
+                        }
+                    }
+                }
+            """
+            assertThat(subject.compileAndLint(code))
+                .hasSize(1)
+                .hasSourceLocations(SourceLocation(3, 9))
+        }
+
         @Nested
         inner class `the statement is a lambda expression` {
             @Test