diff --git a/vlib/v/checker/tests/orm_unused_var.out b/vlib/v/checker/tests/orm_unused_var.out new file mode 100644 index 00000000000000..c23ae3bbf445c4 --- /dev/null +++ b/vlib/v/checker/tests/orm_unused_var.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/orm_unused_var.vv:15:2: warning: unused variable: `x_id` + 13 | + 14 | x := Test{} + 15 | x_id := sql db { + | ~~~~ + 16 | insert x into Test + 17 | } or { panic(err) } diff --git a/vlib/v/checker/tests/orm_unused_var.vv b/vlib/v/checker/tests/orm_unused_var.vv new file mode 100644 index 00000000000000..3921f8ef109f70 --- /dev/null +++ b/vlib/v/checker/tests/orm_unused_var.vv @@ -0,0 +1,18 @@ +import db.sqlite + +struct Test { + id int @[primary; serial] +} + +fn main() { + db := sqlite.connect(':memory:')! + + sql db { + create table Test + }! + + x := Test{} + x_id := sql db { + insert x into Test + } or { panic(err) } +} diff --git a/vlib/v/compiler_errors_test.v b/vlib/v/compiler_errors_test.v index e0190820839903..44633996ee9b6a 100644 --- a/vlib/v/compiler_errors_test.v +++ b/vlib/v/compiler_errors_test.v @@ -20,6 +20,7 @@ const skip_on_ubuntu_musl = [ 'vlib/v/checker/tests/vweb_tmpl_used_var.vv', 'vlib/v/checker/tests/vweb_routing_checks.vv', 'vlib/v/checker/tests/orm_op_with_option_and_none.vv', + 'vlib/v/checker/tests/orm_unused_var.vv', 'vlib/v/tests/skip_unused/gg_code.vv', ] diff --git a/vlib/v/parser/orm.v b/vlib/v/parser/orm.v index e4d9ff7fd17381..d6b516b99e4340 100644 --- a/vlib/v/parser/orm.v +++ b/vlib/v/parser/orm.v @@ -30,6 +30,7 @@ fn (mut p Parser) sql_expr() ast.Expr { mut is_count := false if is_insert { inserted_var = p.check_name() + p.scope.mark_var_as_used(inserted_var) into := p.check_name() if into != 'into' { p.error('expecting `into`')