1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: add error for variables, that are evaluated, but not used

This commit is contained in:
Kris Cherven 2020-05-11 04:13:36 -04:00 committed by GitHub
parent 3a3d00ac72
commit 8bc0c31f29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 0 deletions

View File

@ -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 | }

View File

@ -0,0 +1,7 @@
const (
c = 1
)
fn main() {
c
}

View File

@ -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 | }

View File

@ -0,0 +1,9 @@
const (
c = 1
)
fn main() {
{
c
}
}

View File

@ -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 or_stmts := []ast.Stmt{}
mut is_or_block_used := false mut is_or_block_used := false
if p.tok.kind == .key_orelse { if p.tok.kind == .key_orelse {
p.inside_or_expr = true
p.next() p.next()
p.open_scope() p.open_scope()
p.scope.register('err', ast.Var{ 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 is_or_block_used = true
or_stmts = p.parse_block_no_scope() or_stmts = p.parse_block_no_scope()
p.close_scope() p.close_scope()
p.inside_or_expr = false
} }
node := ast.CallExpr{ node := ast.CallExpr{
name: fn_name name: fn_name

View File

@ -8,6 +8,8 @@ import v.table
import v.token import v.token
fn (mut p Parser) if_expr() ast.IfExpr { fn (mut p Parser) if_expr() ast.IfExpr {
p.inside_if_expr = true
defer { p.inside_if_expr = false }
pos := p.tok.position() pos := p.tok.position()
mut branches := []ast.IfBranch{} mut branches := []ast.IfBranch{}
mut has_else := false mut has_else := false

View File

@ -26,6 +26,8 @@ mut:
is_c bool is_c bool
is_js bool is_js bool
inside_if bool inside_if bool
inside_if_expr bool
inside_or_expr bool
inside_for bool inside_for bool
inside_fn bool inside_fn bool
pref &pref.Preferences 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 { } 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()) 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() epos := p.tok.position()
expr := p.expr(0) expr := p.expr(0)