# Why compiler allow to put unreachable code after return?

**URL:** <https://discuss.kotlinlang.org/t/why-compiler-allow-to-put-unreachable-code-after-return/1436>\
**Category:** Language Design\
**Created:** [January 28, 2016, 12:23pm UTC](https://discuss.kotlinlang.org/t/why-compiler-allow-to-put-unreachable-code-after-return/1436 "2016-01-28T12:23:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jesi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jesi/32/1440_2.png) [@jesi](https://discuss.kotlinlang.org/u/jesi)\
**Post date:** [January 28, 2016, 12:23pm UTC](https://discuss.kotlinlang.org/t/why-compiler-allow-to-put-unreachable-code-after-return/1436/1 "2016-01-28T12:23:10Z")

</div>

In any function I can put `return` and any code after it. Even IDE highlight this code as warning and compiler generates ‘Unreachable code’ message - why compiler allow this possibility?

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [January 28, 2016, 12:37pm UTC](https://discuss.kotlinlang.org/t/why-compiler-allow-to-put-unreachable-code-after-return/1436/2 "2016-01-28T12:37:31Z")

</div>

What would be the exact benefit of changing this warning to an error?

---

<div class="post-metadata">

**Author:** ![jesi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jesi/32/1440_2.png) [@jesi](https://discuss.kotlinlang.org/u/jesi)\
**Post date:** [January 28, 2016, 1:24pm UTC](https://discuss.kotlinlang.org/t/why-compiler-allow-to-put-unreachable-code-after-return/1436/3 "2016-01-28T13:24:07Z")

</div>

I think it have to be less possibilities to write unreachable code). At some moment someone can put wrong `return` and disable all code after it and doesn’t see warning, and break code logic.  
If this possibility introduce some benefits or will introduce in future - it’s ok(just interesting - what kind they are). But if there is no any, even minor, advantages - just why?  
This is not a major problem, and if current variant is just a result of current implementation - OK, but I think it’ll be better to prohibit unreachable code after return.

I like Kotlin, but some time ago, when I told about it with my friend - he asked me the same question, and I didn’t find anything to answer. Some people may feel vague misgivings when find useless possibilities when meet them in new language.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [January 28, 2016, 1:33pm UTC](https://discuss.kotlinlang.org/t/why-compiler-allow-to-put-unreachable-code-after-return/1436/4 "2016-01-28T13:33:58Z")

</div>

I think that if someone puts a “return” statement in the middle of their code, it’s much more likely that they did this intentionally to debug the behavior of the code in a certain scenario, and not accidentally without knowing that this would disable the execution of a part of their code. And if this is indeed accidental, then they will see the warning.

Generally speaking, Kotlin does not report errors for code that has an unambiguous meaning and can be correctly executed. If we believe that the code does not make sense, we may report a warning. However, we do not see any benefit in reporting such code as errors.

---

<div class="post-metadata">

**Author:** ![jesi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jesi/32/1440_2.png) [@jesi](https://discuss.kotlinlang.org/u/jesi)\
**Post date:** [January 28, 2016, 1:59pm UTC](https://discuss.kotlinlang.org/t/why-compiler-allow-to-put-unreachable-code-after-return/1436/5 "2016-01-28T13:59:47Z")

</div>

If this situation [Unreachable code?](https://discuss.kotlinlang.org/t/unreachable-code/813) is still actual - we have two different variants of behavior. In one case this is error, in another this is warning.

Yes, Dmitriy, you are right about warn-severity for code that does not make sense. But my argument is that _code after return_ is potential point for bug. This is answer to your question about benefits of moving to errors.  
And from opposite side - I didn’t see any disadvantages when such situation is considered as error by compiler(Java variant).

---

<div class="post-metadata">

**Author:** ![mikehearn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mikehearn/32/1424_2.png) [@mikehearn](https://discuss.kotlinlang.org/u/mikehearn)\
**Post date:** [January 28, 2016, 10:27pm UTC](https://discuss.kotlinlang.org/t/why-compiler-allow-to-put-unreachable-code-after-return/1436/6 "2016-01-28T22:27:42Z")

</div>

The behaviour of javac has annoyed me so many times. Luckily javac is so dumb you can work around it by simply writing “if (true) return;”

Putting return statements in the middle of code to debug it is a very common thing to want. It makes no sense to treat it as an error.

---

<div class="post-metadata">

**Author:** ![jesi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jesi/32/1440_2.png) [@jesi](https://discuss.kotlinlang.org/u/jesi)\
**Post date:** [January 29, 2016, 6:43am UTC](https://discuss.kotlinlang.org/t/why-compiler-allow-to-put-unreachable-code-after-return/1436/7 "2016-01-29T06:43:19Z")

</div>

But javac behavior saves in such common situation:  
if (foo) {  
return foo  
} else {  
-foo = bar  
+return bar  
}  
blabla  
blabla  
blabla  
return foo

And you never write `if (true) return` not for debugging. `if (true)` not so harder to write.

Another non obvious place is missing ‘+’ in multiline strings on return. Like:  
return “I thought It”  
“would be one string”

---

<div class="post-metadata">

**Author:** ![Krupal](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@Krupal](https://discuss.kotlinlang.org/u/Krupal)\
**Post date:** [February 1, 2016, 7:26am UTC](https://discuss.kotlinlang.org/t/why-compiler-allow-to-put-unreachable-code-after-return/1436/8 "2016-02-01T07:26:56Z")

</div>

Unreachable code definetly should not be allowed, this place is very popular bug in other languages. Kotlin already has enough differences with Java. It seems like in the end you want to get absolutely new language so nobody will switch to it like to scala/ceilon/groovy. Please try to be consistent.

P.S. It is the feature that everybody loves in Java.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [February 1, 2016, 12:07pm UTC](https://discuss.kotlinlang.org/t/why-compiler-allow-to-put-unreachable-code-after-return/1436/9 "2016-02-01T12:07:57Z")

</div>

We’re trying to be consistent in applying this principle: “Generally speaking, Kotlin does not report errors for code that has an unambiguous meaning and can be correctly executed.”

As for “very popular bug” and “feature that everybody loves”, I’ll just say “quotation needed”.
