detekt/detekt

New rule: Reports the usages of `<expr1>?.let { <expr2> } ?: run { <single_line_expr> }`

Aperta

#6112 aperta il 19 mag 2023

 (12 commenti) (0 reazioni) (0 assegnatari)Kotlin (834 fork)batch import
help wantedrules

Metriche repository

Star
 (6942 stelle)
Metriche merge PR
 (Merge medio 4g 9h) (65 PR mergiate in 30 g)

Descrizione

Expected Behavior of the rule

Rule should report <expr1>?.let { <expr2> } ?: run { <single_line_expr> } as expression can be simplified as <expr1>?.let { <expr2> } ?: <single_line_expr>. Here there is an unnecessary use of run { } block

uncompliant code

val nullable: Int? = 4
val default = 1
val a = nullable?.let { 0 } ?: run { default }

compliant code

val nullable: Int? = 4
val default = 1
val a = nullable?.let { 0 } ?: default

Context

Got this review in https://github.com/detekt/detekt/pull/5981#discussion_r1197842827 having unnecessary run { } really makes the code more indented and increases the complexity

Guida contributor