checker: fix "unnecessary `()`" error position
parent
cc4090cc74
commit
fa4739794f
|
@ -1630,12 +1630,8 @@ pub fn (c mut Checker) if_expr(node mut ast.IfExpr) table.Type {
|
||||||
}
|
}
|
||||||
node.typ = table.void_type
|
node.typ = table.void_type
|
||||||
for i, branch in node.branches {
|
for i, branch in node.branches {
|
||||||
match branch.cond {
|
if branch.cond is ast.ParExpr {
|
||||||
ast.ParExpr {
|
c.error('unnecessary `()` in an if condition. use `if expr {` instead of `if (expr) {`.', branch.pos)
|
||||||
c.error('unnecessary `()` in an if condition. use `if expr {` instead of `if (expr) {`.',
|
|
||||||
node.pos)
|
|
||||||
}
|
|
||||||
else {}
|
|
||||||
}
|
}
|
||||||
typ := c.expr(branch.cond)
|
typ := c.expr(branch.cond)
|
||||||
if i < node.branches.len - 1 || !node.has_else {
|
if i < node.branches.len - 1 || !node.has_else {
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
vlib/v/checker/tests/inout/unnecessary_parenthesis.v:2:2: error: unnecessary `()` in an if condition. use `if expr {` instead of `if (expr) {`.
|
||||||
|
1| fn main() {
|
||||||
|
2| if (1 == 1) {
|
||||||
|
~~~~~~~~~~~
|
||||||
|
3| println('yeay')
|
||||||
|
4| } else if (1 == 2) {
|
||||||
|
vlib/v/checker/tests/inout/unnecessary_parenthesis.v:4:4: error: unnecessary `()` in an if condition. use `if expr {` instead of `if (expr) {`.
|
||||||
|
2| if (1 == 1) {
|
||||||
|
3| println('yeay')
|
||||||
|
4| } else if (1 == 2) {
|
||||||
|
~~~~~~~~~~~~~~~~
|
||||||
|
5| println("oh no :'(")
|
||||||
|
6| } else if (1 == 3) {
|
||||||
|
vlib/v/checker/tests/inout/unnecessary_parenthesis.v:6:4: error: unnecessary `()` in an if condition. use `if expr {` instead of `if (expr) {`.
|
||||||
|
4| } else if (1 == 2) {
|
||||||
|
5| println("oh no :'(")
|
||||||
|
6| } else if (1 == 3) {
|
||||||
|
~~~~~~~~~~~~~~~~
|
||||||
|
7| println("what's wrong with physics ????")
|
||||||
|
8| }
|
|
@ -0,0 +1,9 @@
|
||||||
|
fn main() {
|
||||||
|
if (1 == 1) {
|
||||||
|
println('yeay')
|
||||||
|
} else if (1 == 2) {
|
||||||
|
println("oh no :'(")
|
||||||
|
} else if (1 == 3) {
|
||||||
|
println("what's wrong with physics ????")
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,7 +13,7 @@ fn (mut p Parser) if_expr() ast.IfExpr {
|
||||||
mut has_else := false
|
mut has_else := false
|
||||||
for p.tok.kind in [.key_if, .key_else] {
|
for p.tok.kind in [.key_if, .key_else] {
|
||||||
p.inside_if = true
|
p.inside_if = true
|
||||||
branch_pos := p.tok.position()
|
start_pos := p.tok.position()
|
||||||
mut comment := ast.Comment{}
|
mut comment := ast.Comment{}
|
||||||
if p.tok.kind == .key_if {
|
if p.tok.kind == .key_if {
|
||||||
p.check(.key_if)
|
p.check(.key_if)
|
||||||
|
@ -28,9 +28,10 @@ fn (mut p Parser) if_expr() ast.IfExpr {
|
||||||
} else {
|
} else {
|
||||||
has_else = true
|
has_else = true
|
||||||
p.inside_if = false
|
p.inside_if = false
|
||||||
|
end_pos := p.prev_tok.position()
|
||||||
branches << ast.IfBranch{
|
branches << ast.IfBranch{
|
||||||
stmts: p.parse_block()
|
stmts: p.parse_block()
|
||||||
pos: branch_pos
|
pos: start_pos.extend(end_pos)
|
||||||
comment: comment
|
comment: comment
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
@ -56,6 +57,7 @@ fn (mut p Parser) if_expr() ast.IfExpr {
|
||||||
} else {
|
} else {
|
||||||
cond = p.expr(0)
|
cond = p.expr(0)
|
||||||
}
|
}
|
||||||
|
end_pos := p.prev_tok.position()
|
||||||
p.inside_if = false
|
p.inside_if = false
|
||||||
stmts := p.parse_block()
|
stmts := p.parse_block()
|
||||||
if is_or {
|
if is_or {
|
||||||
|
@ -64,7 +66,7 @@ fn (mut p Parser) if_expr() ast.IfExpr {
|
||||||
branches << ast.IfBranch{
|
branches << ast.IfBranch{
|
||||||
cond: cond
|
cond: cond
|
||||||
stmts: stmts
|
stmts: stmts
|
||||||
pos: branch_pos
|
pos: start_pos.extend(end_pos)
|
||||||
comment: ast.Comment{}
|
comment: ast.Comment{}
|
||||||
}
|
}
|
||||||
if p.tok.kind != .key_else {
|
if p.tok.kind != .key_else {
|
||||||
|
|
Loading…
Reference in New Issue