If I used a variable to check for boolean, the "!isNumber" is highlighted with the warning "Condition '!isNumber' is always true":
val isNumber = bind.number.isChecked
when (array) {
"A" -> {
if (isNumber) {
return "number"
} else if (!isNumber) {
return "letter"
}
}
However if I used the view directly to check for boolean, there is no warning:
when (acArray) {
"A" -> {
if (bind.number.isChecked) {
return "number"
} else if (!bind.number.isChecked) {
return "letter"
}
}
Your
ifis already checking for thetruevalue ofisNumber, you don't need to explicitly checkisNumberinelseblock if itsfalse, because the opposite oftrueisfalsethat's why you get that warning.Imagine the compiler talking to you:
Compiler:
Edit: Sample scenarios below, class vs local vs top level file scope
Both
classLevelScopeandlocalLevelScopewill give you a warning.But a Top level file scope will not
My assumption for the top level scoped variable is that the compiler cannot determine who would change its value in what thread, class, instance of a class or from any other place because a top level scoped variable can be accessed anywhere unlike the
classLevelScopeor thelocalLevelScopewhich are only accessible outside using their enclosing scope. Yourbind.number.isCheckedmight be a top level scoped variable.If the warning bothers you, you can remove it from your A.S settings
Inspections -> Kotlin -> Probable bugs -> Constant Condition -> Uncheck the box