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

checker: fix the pos information in the warning message, when the label is not used (fix #16146) (#16148)

This commit is contained in:
shove 2022-10-23 00:26:38 +08:00 committed by GitHub
parent 3d4ffe8f28
commit b8b2b4ad0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 10 deletions

View File

@ -1238,6 +1238,8 @@ pub struct GotoLabel {
pub: pub:
name string name string
pos token.Pos pos token.Pos
pub mut:
is_used bool
} }
pub struct GotoStmt { pub struct GotoStmt {

View File

@ -129,7 +129,7 @@ mut:
inside_decl_rhs bool inside_decl_rhs bool
inside_if_guard bool // true inside the guard condition of `if x := opt() {}` inside_if_guard bool // true inside the guard condition of `if x := opt() {}`
comptime_call_pos int // needed for correctly checking use before decl for templates comptime_call_pos int // needed for correctly checking use before decl for templates
goto_labels map[string]int // to check for unused goto labels goto_labels map[string]ast.GotoLabel // to check for unused goto labels
} }
pub fn new_checker(table &ast.Table, pref &pref.Preferences) &Checker { pub fn new_checker(table &ast.Table, pref &pref.Preferences) &Checker {
@ -4035,8 +4035,9 @@ pub fn (mut c Checker) fail_if_unreadable(expr ast.Expr, typ ast.Type, what stri
fn (mut c Checker) goto_label(node ast.GotoLabel) { fn (mut c Checker) goto_label(node ast.GotoLabel) {
// Register a goto label // Register a goto label
if c.goto_labels[node.name] == 0 { if node.name !in c.goto_labels {
c.goto_labels[node.name] = 0 c.goto_labels[node.name] = node
c.goto_labels[node.name].is_used = false
} }
} }
@ -4050,16 +4051,16 @@ pub fn (mut c Checker) goto_stmt(node ast.GotoStmt) {
if c.table.cur_fn != unsafe { nil } && node.name !in c.table.cur_fn.label_names { if c.table.cur_fn != unsafe { nil } && node.name !in c.table.cur_fn.label_names {
c.error('unknown label `$node.name`', node.pos) c.error('unknown label `$node.name`', node.pos)
} }
c.goto_labels[node.name]++ // Register a label use c.goto_labels[node.name].is_used = true // Register a label use
// TODO: check label doesn't bypass variable declarations // TODO: check label doesn't bypass variable declarations
} }
fn (mut c Checker) check_unused_labels() { fn (mut c Checker) check_unused_labels() {
for label, nr_uses in c.goto_labels { for name, label in c.goto_labels {
if nr_uses == 0 { if !label.is_used {
// TODO show label's location // TODO show label's location
c.warn('label `$label` defined and not used', token.Pos{}) c.warn('label `$name` defined and not used', label.pos)
c.goto_labels[label]++ // so that this warning is not shown again c.goto_labels[name].is_used = true // so that this warning is not shown again
} }
} }
} }

View File

@ -1,5 +1,5 @@
vlib/v/checker/tests/unused_label.vv:1:1: warning: label `mylabel` defined and not used vlib/v/checker/tests/unused_label.vv:2:5: warning: label `mylabel` defined and not used
1 | fn main() { 1 | fn main() {
| ^
2 | mylabel: 2 | mylabel:
| ~~~~~~~~
3 | } 3 | }