detekt/detekt

[Request/Discussion] NullableBooleanCheck rule should only check conditional statements

开放

#7,954 创建于 2025年2月14日

 (6 条评论) (4 个反应) (0 位负责人)Kotlin (834 个派生)batch import
help wantedrules

仓库指标

星标
 (6,942 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

Expected Behavior of the rule

NullableBooleanCheck references this Kotlin convention to justify its check (never use ?: boolean on nullable boolean statements). However, the Kotlin code convention explicitly notes conditional statements, where NullableBooleanCheck checks all nullable boolean statements (e.g. val isFlag = object?.booleanFlag).

For conditional statements, I agree that the if(nullableBoolean == true) syntax is more clear; however, for other statements this seems to add confusion. For example:

val isFlag = object?.booleanFlag ?: true

is (subjectively) more clear than:

val isFlag = object?.booleanFlag != false

Context

I'm proposing that we update the NullableBooleanCheck to only check for the nullable boolean values in conditional statements, removing other nullable boolean statements from its scope. If others would like to utilize the same rule for other nullable boolean statements, a separate rule could be added for compartmentalization.

贡献者指南