From 8bc0c31f2981d382fbfb0abebf5d78621259ad09 Mon Sep 17 00:00:00 2001 From: Kris Cherven <50562493+krischerven@users.noreply.github.com> Date: Mon, 11 May 2020 04:13:36 -0400 Subject: [PATCH] parser: add error for variables, that are evaluated, but not used --- vlib/v/checker/tests/var_eval_not_used.out | 6 ++++++ vlib/v/checker/tests/var_eval_not_used.vv | 7 +++++++ vlib/v/checker/tests/var_eval_not_used_scope.out | 7 +++++++ vlib/v/checker/tests/var_eval_not_used_scope.vv | 9 +++++++++ vlib/v/parser/fn.v | 2 ++ vlib/v/parser/if.v | 2 ++ vlib/v/parser/parser.v | 4 ++++ 7 files changed, 37 insertions(+) create mode 100644 vlib/v/checker/tests/var_eval_not_used.out create mode 100644 vlib/v/checker/tests/var_eval_not_used.vv create mode 100644 vlib/v/checker/tests/var_eval_not_used_scope.out create mode 100644 vlib/v/checker/tests/var_eval_not_used_scope.vv diff --git a/vlib/v/checker/tests/var_eval_not_used.out b/vlib/v/checker/tests/var_eval_not_used.out new file mode 100644 index 0000000000..4f1cc44c5a --- /dev/null +++ b/vlib/v/checker/tests/var_eval_not_used.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/var_eval_not_used.v:6:2: error: `c` evaluated but not used + 4 | + 5 | fn main() { + 6 | c + | ^ + 7 | } diff --git a/vlib/v/checker/tests/var_eval_not_used.vv b/vlib/v/checker/tests/var_eval_not_used.vv new file mode 100644 index 0000000000..57642a1f7c --- /dev/null +++ b/vlib/v/checker/tests/var_eval_not_used.vv @@ -0,0 +1,7 @@ +const ( + c = 1 +) + +fn main() { + c +} diff --git a/vlib/v/checker/tests/var_eval_not_used_scope.out b/vlib/v/checker/tests/var_eval_not_used_scope.out new file mode 100644 index 0000000000..a1b1e01a19 --- /dev/null +++ b/vlib/v/checker/tests/var_eval_not_used_scope.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/var_eval_not_used_scope.v:7:3: error: `c` evaluated but not used + 5 | fn main() { + 6 | { + 7 | c + | ^ + 8 | } + 9 | } diff --git a/vlib/v/checker/tests/var_eval_not_used_scope.vv b/vlib/v/checker/tests/var_eval_not_used_scope.vv new file mode 100644 index 0000000000..298e35b250 --- /dev/null +++ b/vlib/v/checker/tests/var_eval_not_used_scope.vv @@ -0,0 +1,9 @@ +const ( + c = 1 +) + +fn main() { + { + c + } +} diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index e95b108b95..5393cac018 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -37,6 +37,7 @@ pub fn (mut p Parser) call_expr(is_c, is_js bool, mod string) ast.CallExpr { mut or_stmts := []ast.Stmt{} mut is_or_block_used := false if p.tok.kind == .key_orelse { + p.inside_or_expr = true p.next() p.open_scope() p.scope.register('err', ast.Var{ @@ -54,6 +55,7 @@ pub fn (mut p Parser) call_expr(is_c, is_js bool, mod string) ast.CallExpr { is_or_block_used = true or_stmts = p.parse_block_no_scope() p.close_scope() + p.inside_or_expr = false } node := ast.CallExpr{ name: fn_name diff --git a/vlib/v/parser/if.v b/vlib/v/parser/if.v index f8e1e21ffd..8f398efcd1 100644 --- a/vlib/v/parser/if.v +++ b/vlib/v/parser/if.v @@ -8,6 +8,8 @@ import v.table import v.token fn (mut p Parser) if_expr() ast.IfExpr { + p.inside_if_expr = true + defer { p.inside_if_expr = false } pos := p.tok.position() mut branches := []ast.IfBranch{} mut has_else := false diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 808d3345b8..4e4babf179 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -26,6 +26,8 @@ mut: is_c bool is_js bool inside_if bool + inside_if_expr bool + inside_or_expr bool inside_for bool inside_fn bool pref &pref.Preferences @@ -479,6 +481,8 @@ pub fn (mut p Parser) stmt() ast.Stmt { } } else if p.tok.kind == .name && p.peek_tok.kind == .name { p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position()) + } else if p.tok.kind == .name && !p.inside_if_expr && !p.inside_or_expr && p.peek_tok.kind in [.rcbr, .eof] { + p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position()) } epos := p.tok.position() expr := p.expr(0)