From c5a7c51dfd4821d51d8ef4fa0b34ee4f1dd959f7 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Thu, 3 Dec 2020 09:16:04 +0530 Subject: [PATCH] parser: add a check for known vars in or block (#7094) --- vlib/v/parser/parser.v | 2 +- vlib/v/tests/option_default_values_test.v | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 7f5dbcbe72..1508a21b85 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -645,7 +645,7 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt { } else if p.peek_tok.kind == .name { p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position()) } else if !p.inside_if_expr && !p.inside_match_body && !p.inside_or_expr && - p.peek_tok.kind in [.rcbr, .eof] { + p.peek_tok.kind in [.rcbr, .eof] && !p.mark_var_as_used(p.tok.lit) { p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position()) } } diff --git a/vlib/v/tests/option_default_values_test.v b/vlib/v/tests/option_default_values_test.v index 4ec908419a..8c103df281 100644 --- a/vlib/v/tests/option_default_values_test.v +++ b/vlib/v/tests/option_default_values_test.v @@ -154,3 +154,13 @@ fn test_nested_optional_with_opt_fn_call_as_last_value() { // } // assert e == false } + +fn remove_suffix1(s string) string { + n := s.len + i := s.last_index('.') or { n } + return s[0..i] +} + +fn test_var_inside_or_block() { + assert remove_suffix1('Vlang.foo') == 'Vlang' +}