Skip to content

Commit 17c34b0

Browse files
authored
checker, cgen: minor cleanup of last expr stmt expression (#14057)
1 parent cb44f59 commit 17c34b0

File tree

6 files changed

+13
-17
lines changed

6 files changed

+13
-17
lines changed

vlib/v/checker/checker.v

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,14 +1533,12 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret
15331533
if stmt.typ == ast.void_type {
15341534
if stmt.expr is ast.IfExpr {
15351535
for branch in stmt.expr.branches {
1536-
last_stmt := branch.stmts[branch.stmts.len - 1]
1537-
c.check_or_last_stmt(last_stmt, ret_type, expr_return_type)
1536+
c.check_or_last_stmt(branch.stmts.last(), ret_type, expr_return_type)
15381537
}
15391538
return
15401539
} else if stmt.expr is ast.MatchExpr {
15411540
for branch in stmt.expr.branches {
1542-
last_stmt := branch.stmts[branch.stmts.len - 1]
1543-
c.check_or_last_stmt(last_stmt, ret_type, expr_return_type)
1541+
c.check_or_last_stmt(branch.stmts.last(), ret_type, expr_return_type)
15441542
}
15451543
return
15461544
}
@@ -1570,14 +1568,12 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret
15701568
match stmt.expr {
15711569
ast.IfExpr {
15721570
for branch in stmt.expr.branches {
1573-
last_stmt := branch.stmts[branch.stmts.len - 1]
1574-
c.check_or_last_stmt(last_stmt, ret_type, expr_return_type)
1571+
c.check_or_last_stmt(branch.stmts.last(), ret_type, expr_return_type)
15751572
}
15761573
}
15771574
ast.MatchExpr {
15781575
for branch in stmt.expr.branches {
1579-
last_stmt := branch.stmts[branch.stmts.len - 1]
1580-
c.check_or_last_stmt(last_stmt, ret_type, expr_return_type)
1576+
c.check_or_last_stmt(branch.stmts.last(), ret_type, expr_return_type)
15811577
}
15821578
}
15831579
else {

vlib/v/checker/if.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
156156
c.smartcast_cond_pos = token.Pos{}
157157
}
158158
if expr_required {
159-
if branch.stmts.len > 0 && branch.stmts[branch.stmts.len - 1] is ast.ExprStmt {
160-
mut last_expr := branch.stmts[branch.stmts.len - 1] as ast.ExprStmt
159+
if branch.stmts.len > 0 && branch.stmts.last() is ast.ExprStmt {
160+
mut last_expr := branch.stmts.last() as ast.ExprStmt
161161
c.expected_type = former_expected_type
162162
if c.expected_type.has_flag(.optional) {
163163
if node.typ == ast.void_type {

vlib/v/checker/match.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
5151
// currently the last statement in a match branch does not have an
5252
// expected value set, so e.g. IfExpr.is_expr is not set.
5353
// probably any mismatch will be caught by not producing a value instead
54-
for st in branch.stmts[0..branch.stmts.len - 1] {
54+
for st in branch.stmts[..branch.stmts.len - 1] {
5555
// must not contain C statements
5656
st.check_c_expr() or {
5757
c.error('`match` expression branch has $err.msg()', st.pos)
@@ -64,7 +64,7 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
6464
}
6565
// If the last statement is an expression, return its type
6666
if branch.stmts.len > 0 {
67-
mut stmt := branch.stmts[branch.stmts.len - 1]
67+
mut stmt := branch.stmts.last()
6868
if mut stmt is ast.ExprStmt {
6969
if node.is_expr {
7070
c.expected_type = node.expected_type

vlib/v/gen/c/comptime.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,14 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) {
263263
if node.is_expr {
264264
len := branch.stmts.len
265265
if len > 0 {
266-
last := branch.stmts[len - 1] as ast.ExprStmt
266+
last := branch.stmts.last() as ast.ExprStmt
267267
if len > 1 {
268268
tmp := g.new_tmp_var()
269269
styp := g.typ(last.typ)
270270
g.indent++
271271
g.writeln('$styp $tmp;')
272272
g.writeln('{')
273-
g.stmts(branch.stmts[0..len - 1])
273+
g.stmts(branch.stmts[..len - 1])
274274
g.write('\t$tmp = ')
275275
g.stmt(last)
276276
g.writeln('}')

vlib/v/gen/c/if.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
148148
} else {
149149
mut is_auto_heap := false
150150
if branch.stmts.len > 0 {
151-
scope := g.file.scope.innermost(ast.Node(branch.stmts[branch.stmts.len - 1]).pos().pos)
151+
scope := g.file.scope.innermost(ast.Node(branch.stmts.last()).pos().pos)
152152
if v := scope.find_var(branch.cond.vars[0].name) {
153153
is_auto_heap = v.is_auto_heap
154154
}

vlib/v/gen/js/comptime.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ fn (mut g JsGen) comptime_if(node ast.IfExpr) {
2828
print('$branch.stmts')
2929
len := branch.stmts.len
3030
if len > 0 {
31-
last := branch.stmts[len - 1] as ast.ExprStmt
31+
last := branch.stmts.last() as ast.ExprStmt
3232
if len > 1 {
3333
tmp := g.new_tmp_var()
3434
g.inc_indent()
3535
g.writeln('let $tmp;')
3636
g.writeln('{')
37-
g.stmts(branch.stmts[0..len - 1])
37+
g.stmts(branch.stmts[..len - 1])
3838
g.write('\t$tmp = ')
3939
g.stmt(last)
4040
g.writeln('}')

0 commit comments

Comments
 (0)