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

yuyi 2022-04-20 19:23:32 +08:00 committed by Jef Roosens
parent 9abf3a62c0
commit 81a178ee8d
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
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() }`
mut ret_type := ast.void_type
if node.stmts.len > 0 {
last_stmt := node.stmts[node.stmts.len - 1]
last_stmt := node.stmts.last()
if last_stmt is ast.ExprStmt {
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
// Add return if `fn(...) ? {...}` have no return at end
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)
if sym.kind == .void {
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
if node.branches.len > 0 && node.has_else {
stmts := node.branches[0].stmts
if stmts.len > 0 && stmts[stmts.len - 1] is ast.ExprStmt
&& (stmts[stmts.len - 1] as ast.ExprStmt).typ != ast.void_type {
if stmts.len > 0 && stmts.last() is ast.ExprStmt
&& (stmts.last() as ast.ExprStmt).typ != ast.void_type {
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
}
stmts := or_block.stmts
if stmts.len > 0 && stmts[or_block.stmts.len - 1] is ast.ExprStmt
&& (stmts[stmts.len - 1] as ast.ExprStmt).typ != ast.void_type {
if stmts.len > 0 && stmts.last() is ast.ExprStmt
&& (stmts.last() as ast.ExprStmt).typ != ast.void_type {
g.indent++
for i, stmt in stmts {
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--
} else {
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(';')
}
}