return is nested inside the block. It is not at the same nesting level as MsgBox.
For one, if it was at the same nesting level, you could do this:
A control flow statement such as If or Loop takes a single statement as its body. You can use an entire block because the content of the block (and its block-end) is nested under the block-begin, allowing the overall block to act as a compound statement. Lines are grouped within blocks this way regardless of what happens to be above the block.
Also, the warning is shown "for each line that
immediately follows a Return [etc.]". In your example, the line immediately following
return is
}, but common sense says it must be permitted. (In this case common sense might say to not use a block at all, but if there's a warning, it should be about that and not
} being unreachable.) The exception is that function definitions are skipped; otherwise, only the line immediately after the
return is considered.
Also, "this does not detect all unreachable code".
The detection is intentionally simplistic, to minimize code size and complexity and the risk of showing warnings for legitimate code. All block-ends are treated the same, without regard to where the corresponding block-begin is or what is above it.
Compilers of other languages would tend to eliminate the braces during compilation, but that doesn't happen here, as you can see by checking
ListLines. Those compilers do a lot more work (for other reasons than detecting unreachable code), breaking the code into smaller units and analyzing each code path.