79269310

Date: 2024-12-10 18:14:20
Score: 0.5
Natty:
Report link

None of the answers really explain the problem. The real issue here is the exception code

} catch (e: Exception) {
    e.message?.let { Log.e(Constants.TAG, e.message!!) }
}

This entire expression evaluates to returning Boolean?. Why? It returns null (not Unit) when e.message is null. Otherwise it returns the Boolean from Log.e() because Log.e() returns a Boolean. Kotlin treats any final expression that evaluates to something other than Unit as a return expression. That means the entire function has an implied Boolean? return value.

There are a number of ways to address this. Calling return after the log expression is the easiest fix. This explicitly tells the compiler that nothing should be returned.

I frequently get caught out by this because the logging functions returns a Boolean value or when using the ?. operator on the last line of a conditional expression.

Reasons:
  • Blacklisted phrase (0.5): Why?
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (0.5):
Posted by: Colorado.Rob