1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: warn when using goto outside of unsafe (#8741)

This commit is contained in:
Nick Treleaven
2021-02-15 13:48:24 +00:00
committed by GitHub
parent 6781f732f4
commit 629d43caf5
6 changed files with 39 additions and 32 deletions

View File

@ -3910,21 +3910,25 @@ fn C.DefWindowProc(hwnd int, msg int, lparam int, wparam int)
V allows unconditionally jumping to a label with `goto`. The label name must be contained
within the same function as the `goto` statement. A program may `goto` a label outside
or deeper than the current scope, but it must not skip a variable initialization.
or deeper than the current scope. `goto` allows jumping past variable initialization or
jumping back to code that accesses memory that has already been freed, so it requires
`unsafe`.
```v ignore
if x {
// ...
if y {
goto my_label
unsafe {
goto my_label
}
}
// ...
}
my_label:
```
`goto` should be avoided when `for` can be used instead. In particular,
[labelled break](#labelled-break--continue) can be used to break out of
a nested loop.
`goto` should be avoided, particularly when `for` can be used instead.
[Labelled break/continue](#labelled-break--continue) can be used to break out of
a nested loop, and those do not risk violating memory-safety.
# Appendices