From ae0fdbd834b2f0a49fce25f21275bf772004161c Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 19 Nov 2024 08:36:26 +0200 Subject: [PATCH] v.markused: process the init statements in `for init; cond; inc {` too (#22908) --- vlib/v/markused/walker.v | 15 ++++++++++++--- vlib/v/tests/skip_unused/for_c_stmt.run.out | 5 +++++ .../skip_unused/for_c_stmt.skip_unused.run.out | 5 +++++ vlib/v/tests/skip_unused/for_c_stmt.vv | 10 ++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/skip_unused/for_c_stmt.run.out create mode 100644 vlib/v/tests/skip_unused/for_c_stmt.skip_unused.run.out create mode 100644 vlib/v/tests/skip_unused/for_c_stmt.vv diff --git a/vlib/v/markused/walker.v b/vlib/v/markused/walker.v index 8abcc6666e..de28a219e1 100644 --- a/vlib/v/markused/walker.v +++ b/vlib/v/markused/walker.v @@ -154,8 +154,15 @@ pub fn (mut w Walker) stmt(node_ ast.Stmt) { w.fn_decl(mut node) } ast.ForCStmt { - w.expr(node.cond) - w.stmt(node.inc) + if node.has_init { + w.stmt(node.init) + } + if node.has_cond { + w.expr(node.cond) + } + if node.has_inc { + w.stmt(node.inc) + } w.stmts(node.stmts) } ast.ForInStmt { @@ -179,7 +186,9 @@ pub fn (mut w Walker) stmt(node_ ast.Stmt) { } } ast.ForStmt { - w.expr(node.cond) + if !node.is_inf { + w.expr(node.cond) + } w.stmts(node.stmts) } ast.Return { diff --git a/vlib/v/tests/skip_unused/for_c_stmt.run.out b/vlib/v/tests/skip_unused/for_c_stmt.run.out new file mode 100644 index 0000000000..f1a3a7c919 --- /dev/null +++ b/vlib/v/tests/skip_unused/for_c_stmt.run.out @@ -0,0 +1,5 @@ +ok +12 +13 +14 +ok diff --git a/vlib/v/tests/skip_unused/for_c_stmt.skip_unused.run.out b/vlib/v/tests/skip_unused/for_c_stmt.skip_unused.run.out new file mode 100644 index 0000000000..f1a3a7c919 --- /dev/null +++ b/vlib/v/tests/skip_unused/for_c_stmt.skip_unused.run.out @@ -0,0 +1,5 @@ +ok +12 +13 +14 +ok diff --git a/vlib/v/tests/skip_unused/for_c_stmt.vv b/vlib/v/tests/skip_unused/for_c_stmt.vv new file mode 100644 index 0000000000..c45363f52b --- /dev/null +++ b/vlib/v/tests/skip_unused/for_c_stmt.vv @@ -0,0 +1,10 @@ +const abc = 12 + +fn main() { + println('ok') + for x := abc; x<15;x++ { + println(x) + } + println('ok') +} +