Why does this compile? ``` private fun shouldN...
# announcements
g
Why does this compile?
Copy code
private fun shouldNotCompile(optionalString: String?): Boolean {
        optionalString?.let {
            return true
        }
    }
I’ve completely forgotten to specify a return value for the case where the
let
is not entered.
🐛 1
e
Weeeell, it might be considered a bug, but... Compiler optimizes it to
Copy code
boolean shouldNotCompile(@Nullable String optionalString) {
  return optionalString != null
}
Probably expected behavior due to optimizations.
a
Is what appears to be happening, but why would it do that though?
e
No, it's a bug.
Copy code
fun shouldNotCompile(optionalString: String?): Boolean {
    optionalString?.let {
        println("something")
        return true
    }
}
Decompiles to
Copy code
boolean shouldNotCompile(@Nullable String optionalString) {
      if (optionalString != null) {
         String var3 = "something";
         System.out.println(var3);
         return true;
      } else {
         return false;
      }
   }
Which sure should not happen.
i
Yes, it's a bug related to contracts and control flow analysis https://youtrack.jetbrains.com/issue/KT-28061