Skip to content

Commit

Permalink
orm: fix mark as used var on insert statement (fix vlang#23032) (vlan…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 3, 2024
1 parent 1eb3867 commit 31ce668
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/orm_unused_var.out
Original file line number Diff line number Diff line change
@@ -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) }
18 changes: 18 additions & 0 deletions vlib/v/checker/tests/orm_unused_var.vv
Original file line number Diff line number Diff line change
@@ -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) }
}
1 change: 1 addition & 0 deletions vlib/v/compiler_errors_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]

Expand Down
1 change: 1 addition & 0 deletions vlib/v/parser/orm.v
Original file line number Diff line number Diff line change
Expand Up @@ -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`')
Expand Down

0 comments on commit 31ce668

Please sign in to comment.