Because it is almost impossible to detect!
In the example you pointed out, it’s obvious (to us) that the code will loop forever. But the compiler just sees the function call; at that time, it does not always know what calls this function, what conditional logic can change the behavior of the loop, etc.
For example, with this small change, you are no longer in an infinite loop:
private bool method1called = false; private void method1() { MessageBox.Show("method1 called, which will now call method2"); if (!method1called) method2(); method1called = true; } private void method2() { MessageBox.Show("method2 called, which will now call method1"); method1(); }
Without actually starting the program, how do you know that this is not a cycle? I might see a warning for while (true) , but this has enough valid use cases, which also makes sense not to enter a warning for it.
The compiler simply parses the code and translates to IL (for .NET anyway). You can get limited information, such as variables that are not assigned at the same time (especially because they must generate a character table anyway), but advanced detection like this is usually provided to code analysis tools.
source share