From 9b4c81e85f67f5433d8061e7e77d9f225dcd4589 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 11 Jul 2020 22:30:49 +0800 Subject: [PATCH] checker: check missing return values in functions (#5803) --- vlib/v/checker/checker.v | 4 ++-- vlib/v/checker/tests/function_missing_return_type.out | 5 +++++ vlib/v/checker/tests/function_missing_return_type.vv | 7 +++++++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/function_missing_return_type.out create mode 100644 vlib/v/checker/tests/function_missing_return_type.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index a74144f0e9..893bff9499 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3060,7 +3060,7 @@ fn (c &Checker) fetch_and_verify_orm_fields(info table.Struct, pos token.Positio } fn (mut c Checker) fn_decl(mut node ast.FnDecl) { - c.returns = true + c.returns = false if node.is_generic && c.cur_generic_type == 0 { // need the cur_generic_type check to avoid inf. recursion // loop thru each generic type and generate a function for gen_type in c.table.fn_gen_types[node.name] { @@ -3150,7 +3150,7 @@ fn has_top_return(stmts []ast.Stmt) bool { if stmts.filter(it is ast.Return).len > 0 { return true } - mut has_unsafe_return := false + mut has_unsafe_return := false for _, stmt in stmts { if stmt is ast.UnsafeStmt { for ustmt in stmt.stmts { diff --git a/vlib/v/checker/tests/function_missing_return_type.out b/vlib/v/checker/tests/function_missing_return_type.out new file mode 100644 index 0000000000..7c15bd1854 --- /dev/null +++ b/vlib/v/checker/tests/function_missing_return_type.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/function_missing_return_type.v:1:1: error: missing return at end of function `h` + 1 | fn h() int { + | ~~~~~~~~~~ + 2 | } + 3 | diff --git a/vlib/v/checker/tests/function_missing_return_type.vv b/vlib/v/checker/tests/function_missing_return_type.vv new file mode 100644 index 0000000000..adaf7b5a5d --- /dev/null +++ b/vlib/v/checker/tests/function_missing_return_type.vv @@ -0,0 +1,7 @@ +fn h() int { +} + +fn main() { + d := h() + println('$d') +}