checker, cgen: use 'stmts.last()' instead of 'stmts[stmts.len - 1]' (#14105)
							parent
							
								
									9abf3a62c0
								
							
						
					
					
						commit
						81a178ee8d
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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(';')
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue