From ba091a36dd722cf47b426f5716d07a35beb5331f Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Thu, 12 Jan 2023 10:36:44 -0300 Subject: [PATCH] checker: fix unreachable code checking for sql ORM blocks (#16948) --- cmd/tools/vtest-self.v | 2 ++ vlib/v/checker/orm.v | 4 +-- .../orm_stmt_wrong_return_checking_test.v | 29 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/orm_stmt_wrong_return_checking_test.v diff --git a/cmd/tools/vtest-self.v b/cmd/tools/vtest-self.v index 3a8728a08e..2c5f7ca27f 100644 --- a/cmd/tools/vtest-self.v +++ b/cmd/tools/vtest-self.v @@ -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', @@ -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', diff --git a/vlib/v/checker/orm.v b/vlib/v/checker/orm.v index 8836701184..5a0d8e271f 100644 --- a/vlib/v/checker/orm.v +++ b/vlib/v/checker/orm.v @@ -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 } diff --git a/vlib/v/tests/orm_stmt_wrong_return_checking_test.v b/vlib/v/tests/orm_stmt_wrong_return_checking_test.v new file mode 100644 index 0000000000..27b5380305 --- /dev/null +++ b/vlib/v/tests/orm_stmt_wrong_return_checking_test.v @@ -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 +}