checker, cgen: use 'stmts.last()' instead of 'stmts[stmts.len - 1]' (#14105)

master
yuyi 2022-04-20 19:23:32 +08:00 committed by GitHub
parent 44ba19716b
commit ce4c2afc9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 7 deletions

View File

@ -3497,7 +3497,7 @@ pub fn (mut c Checker) lock_expr(mut node ast.LockExpr) ast.Type {
// handle `x := rlock a { a.getval() }` // handle `x := rlock a { a.getval() }`
mut ret_type := ast.void_type mut ret_type := ast.void_type
if node.stmts.len > 0 { if node.stmts.len > 0 {
last_stmt := node.stmts[node.stmts.len - 1] last_stmt := node.stmts.last()
if last_stmt is ast.ExprStmt { if last_stmt is ast.ExprStmt {
ret_type = last_stmt.typ ret_type = last_stmt.typ
} }

View File

@ -302,7 +302,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
// c.table.cur_fn = node // c.table.cur_fn = node
// Add return if `fn(...) ? {...}` have no return at end // Add return if `fn(...) ? {...}` have no return at end
if node.return_type != ast.void_type && node.return_type.has_flag(.optional) if node.return_type != ast.void_type && node.return_type.has_flag(.optional)
&& (node.stmts.len == 0 || node.stmts[node.stmts.len - 1] !is ast.Return) { && (node.stmts.len == 0 || node.stmts.last() !is ast.Return) {
sym := c.table.sym(node.return_type) sym := c.table.sym(node.return_type)
if sym.kind == .void { if sym.kind == .void {
node.stmts << ast.Return{ node.stmts << ast.Return{

View File

@ -11,8 +11,8 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
mut node_is_expr := false mut node_is_expr := false
if node.branches.len > 0 && node.has_else { if node.branches.len > 0 && node.has_else {
stmts := node.branches[0].stmts stmts := node.branches[0].stmts
if stmts.len > 0 && stmts[stmts.len - 1] is ast.ExprStmt if stmts.len > 0 && stmts.last() is ast.ExprStmt
&& (stmts[stmts.len - 1] as ast.ExprStmt).typ != ast.void_type { && (stmts.last() as ast.ExprStmt).typ != ast.void_type {
node_is_expr = true node_is_expr = true
} }
} }

View File

@ -4943,8 +4943,8 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty
g.inside_or_block = false g.inside_or_block = false
} }
stmts := or_block.stmts stmts := or_block.stmts
if stmts.len > 0 && stmts[or_block.stmts.len - 1] is ast.ExprStmt if stmts.len > 0 && stmts.last() is ast.ExprStmt
&& (stmts[stmts.len - 1] as ast.ExprStmt).typ != ast.void_type { && (stmts.last() as ast.ExprStmt).typ != ast.void_type {
g.indent++ g.indent++
for i, stmt in stmts { for i, stmt in stmts {
if i == stmts.len - 1 { if i == stmts.len - 1 {
@ -4966,7 +4966,7 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty
g.indent-- g.indent--
} else { } else {
g.stmts(stmts) g.stmts(stmts)
if stmts.len > 0 && stmts[or_block.stmts.len - 1] is ast.ExprStmt { if stmts.len > 0 && stmts.last() is ast.ExprStmt {
g.writeln(';') g.writeln(';')
} }
} }