cgen: remove the extra generated parentheses of single in_expr (fix #12158) (#12168)

pull/12180/head
yuyi 2021-10-14 16:38:16 +08:00 committed by GitHub
parent 6f629d1a6a
commit 05885059bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 2 deletions

View File

@ -5051,9 +5051,24 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
}
}
else {
g.write('if (')
mut no_needs_par := false
if branch.cond is ast.InfixExpr {
if branch.cond.op == .key_in && branch.cond.left !is ast.InfixExpr
&& branch.cond.right is ast.ArrayInit {
no_needs_par = true
}
}
if no_needs_par {
g.write('if ')
} else {
g.write('if (')
}
g.expr(branch.cond)
g.writeln(') {')
if no_needs_par {
g.writeln(' {')
} else {
g.writeln(') {')
}
}
}
}