detekt/detekt

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

オープン

#6,112 opened on 2023/05/19

 (12 件のコメント) (0 件のリアクション) (0 人の担当者)Kotlin (834 件のフォーク)batch import
help wantedrules

Repository metrics

Stars
 (6,942 個のスター)
PR merge metrics
 (平均マージ 4d 9h) (30d で 65 merged PRs)

説明

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

コントリビューターガイド