Skip to content

Commit

Permalink
Add various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalyVPinchuk committed Apr 7, 2023
1 parent 191644c commit bbc41bf
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 153 deletions.
Expand Up @@ -48,13 +48,15 @@ import org.jetbrains.kotlin.psi.psiUtil.siblings
* }
* // singleLine = 'necessary'
* when (a) {
* 1 -> { f1() }
* 1 -> { f1() } // unnecessary braces
* 2 -> f2()
* }
* // multiLine = 'necessary'
* when (a) {
* 1 -> {
* 1 -> { // unnecessary braces
* f1()
* }
* 2 -> f2()
* }
*
* // singleLine = 'consistent'
Expand Down Expand Up @@ -104,13 +106,13 @@ import org.jetbrains.kotlin.psi.psiUtil.siblings
* // singleLine = 'necessary'
* when (a) {
* 1 -> f1()
* 2 -> f2()
* 2 -> { f2(); f3() } // necessary braces because of multiple statements
* }
* // multiLine = 'necessary'
* when (a) {
* 1 ->
* f1()
* 2 -> {
* 2 -> { // // necessary braces because of multiple statements
* f2()
* f3()
* }
Expand Down Expand Up @@ -204,7 +206,7 @@ class BracesOnWhenStatements(config: Config = Config.empty) : Rule(config) {
private fun KtWhenEntry.hasBraces(): Boolean = expression is KtBlockExpression

private fun KtWhenEntry.isMultiStatement(): Boolean =
expression.let { (it is KtBlockExpression) && (it.statements.size > 1) }
expression.let { it is KtBlockExpression && it.statements.size > 1 }

private fun policy(expression: KtWhenExpression): BracePolicy {
val isMultiLine = expression.entries.any { branch ->
Expand All @@ -220,23 +222,19 @@ class BracesOnWhenStatements(config: Config = Config.empty) : Rule(config) {
val parent = violator.parent as KtWhenExpression
val reported = when {
violator in parent.entries && policy == BracePolicy.Consistent -> parent.whenKeyword
violator in parent.entries -> violator.arrow
violator in parent.entries -> requireNotNull(violator.arrow) {
"When branch ${violator.text} has no arrow!"

Check warning on line 226 in detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/BracesOnWhenStatements.kt

View check run for this annotation

Codecov / codecov/patch

detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/BracesOnWhenStatements.kt#L226

Added line #L226 was not covered by tests
}
else -> error("Violating element (${violator.text}) is not part of this 'when' (${parent.text})")

Check warning on line 228 in detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/BracesOnWhenStatements.kt

View check run for this annotation

Codecov / codecov/patch

detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/BracesOnWhenStatements.kt#L228

Added line #L228 was not covered by tests
} ?: return
val message = when (policy) {
BracePolicy.Always -> "Missing braces on this branch, add them."
BracePolicy.Consistent -> "Inconsistent braces, make sure all branches either have or don't have braces."
BracePolicy.Necessary -> "Extra braces exist on this branch, remove them."
BracePolicy.Never -> "Extra braces exist on this branch, remove them."
}
report(CodeSmell(issue, Entity.from(reported), message))
report(CodeSmell(issue, Entity.from(reported), policy.message))
}

enum class BracePolicy(val config: String) {
Always("always"),
Consistent("consistent"),
Necessary("necessary"),
Never("never");
enum class BracePolicy(val config: String, val message: String) {
Always("always", "Missing braces on this branch, add them."),
Consistent("consistent", "Inconsistent braces, make sure all branches either have or don't have braces."),
Necessary("necessary", "Extra braces exist on this branch, remove them."),
Never("never", "Extra braces exist on this branch, remove them.");

companion object {
fun getValue(arg: String): BracePolicy =
Expand Down

0 comments on commit bbc41bf

Please sign in to comment.