Skip to content

Commit

Permalink
checker: fix unreachable code checking for sql ORM blocks (#16948)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Jan 12, 2023
1 parent 33191e4 commit ba091a3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/tools/vtest-self.v
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const (
'vlib/v/tests/orm_sub_array_struct_test.v',
'vlib/v/tests/orm_joined_tables_select_test.v',
'vlib/v/tests/sql_statement_inside_fn_call_test.v',
'vlib/v/tests/orm_stmt_wrong_return_checking_test.v',
'vlib/vweb/tests/vweb_test.v',
'vlib/vweb/csrf/csrf_test.v',
'vlib/vweb/request_test.v',
Expand Down Expand Up @@ -177,6 +178,7 @@ const (
'vlib/v/tests/orm_sub_struct_test.v',
'vlib/v/tests/orm_sub_array_struct_test.v',
'vlib/v/tests/orm_joined_tables_select_test.v',
'vlib/v/tests/orm_stmt_wrong_return_checking_test.v',
'vlib/v/tests/sql_statement_inside_fn_call_test.v',
'vlib/clipboard/clipboard_test.v',
'vlib/vweb/tests/vweb_test.v',
Expand Down
4 changes: 1 addition & 3 deletions vlib/v/checker/orm.v
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ fn (mut c Checker) sql_stmt(mut node ast.SqlStmt) ast.Type {
}
}
if node.or_expr.kind == .block {
for s in node.or_expr.stmts {
c.stmt(s)
}
c.stmts_ending_with_expression(node.or_expr.stmts)
}
return typ
}
Expand Down
29 changes: 29 additions & 0 deletions vlib/v/tests/orm_stmt_wrong_return_checking_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sqlite

struct Target {
pub mut:
id int [primary; sql: serial]
kind string [nonull]
}

fn add_target(repo Target) !int {
mut dbs := sqlite.connect('test.db')!
defer {
dbs.close() or { panic(err) }
}
sql dbs {
insert repo into Target
} or {
println(err)
return 1
}

assert true

return 1
}

fn test_main() {
add_target(Target{1, 'foo'}) or { println('>> ${err}') }
assert true
}

0 comments on commit ba091a3

Please sign in to comment.