1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: replace x[x.len-1] with x.last() (#16296)

This commit is contained in:
shove 2022-11-03 03:08:33 +08:00 committed by GitHub
parent d11baa691c
commit dddcf423db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 31 additions and 31 deletions

View File

@ -996,8 +996,8 @@ pub fn (mut c Checker) check_or_expr(node ast.OrExpr, ret_type ast.Type, expr_re
}
return
}
stmts_len := node.stmts.len
if stmts_len == 0 {
if node.stmts.len == 0 {
if ret_type != ast.void_type {
// x := f() or {}
c.error('assignment requires a non empty `or {}` block', node.pos)
@ -1005,7 +1005,7 @@ pub fn (mut c Checker) check_or_expr(node ast.OrExpr, ret_type ast.Type, expr_re
// allow `f() or {}`
return
}
last_stmt := node.stmts[stmts_len - 1]
last_stmt := node.stmts.last()
c.check_or_last_stmt(last_stmt, ret_type, expr_return_type.clear_flag(.optional).clear_flag(.result))
}
@ -1537,7 +1537,7 @@ pub fn (mut c Checker) enum_decl(mut node ast.EnumDecl) {
} else {
if signed {
if iseen.len > 0 {
ilast := iseen[iseen.len - 1]
ilast := iseen.last()
if ilast == enum_imax {
c.error('enum value overflows type `$senum_type`, which has a maximum value of $enum_imax',
field.pos)
@ -1551,7 +1551,7 @@ pub fn (mut c Checker) enum_decl(mut node ast.EnumDecl) {
}
} else {
if useen.len > 0 {
ulast := useen[useen.len - 1]
ulast := useen.last()
if ulast == enum_umax {
c.error('enum value overflows type `$senum_type`, which has a maximum value of $enum_umax',
field.pos)

View File

@ -925,7 +925,7 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
}
param := if func.is_variadic && i >= func.params.len - 1 {
func.params[func.params.len - 1]
func.params.last()
} else {
func.params[i]
}
@ -1182,7 +1182,7 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
if func.generic_names.len > 0 {
for i, mut call_arg in node.args {
param := if func.is_variadic && i >= func.params.len - 1 {
func.params[func.params.len - 1]
func.params.last()
} else {
func.params[i]
}

View File

@ -338,7 +338,7 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym
}
}
}
mut else_branch := node.branches[node.branches.len - 1]
mut else_branch := node.branches.last()
mut has_else := else_branch.is_else
if !has_else {
for i, branch in node.branches {

View File

@ -148,7 +148,7 @@ pub fn (graph &DepGraph) resolve() &DepGraph {
}
pub fn (graph &DepGraph) last_node() DepGraphNode {
return graph.nodes[graph.nodes.len - 1]
return graph.nodes.last()
}
pub fn (graph &DepGraph) display() string {

View File

@ -132,7 +132,7 @@ pub fn (mut f Fmt) wrap_long_line(penalty_idx int, add_indent bool) bool {
if penalty_idx > 0 && f.line_len <= fmt.max_len[penalty_idx] {
return false
}
if f.out[f.out.len - 1] == ` ` {
if f.out.last() == ` ` {
f.out.go_back(1)
}
f.write('\n')
@ -255,7 +255,7 @@ pub fn (mut f Fmt) short_module(name string) string {
}
idx := vals.len - 1
mname, tprefix := f.get_modname_prefix(vals[..idx].join('.'))
symname := vals[vals.len - 1]
symname := vals.last()
mut aname := f.mod2alias[mname]
if aname == '' {
for _, v in f.mod2alias {
@ -1955,7 +1955,7 @@ pub fn (mut f Fmt) ident(node ast.Ident) {
// "v.fmt.foo" => "fmt.foo"
vals := full_name.split('.')
mod_prefix := vals[vals.len - 2]
const_name := vals[vals.len - 1]
const_name := vals.last()
if mod_prefix == 'main' {
f.write(const_name)
return

View File

@ -264,7 +264,7 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
fields_start := f.out.len
fields_loop: for {
if !single_line_fields {
if use_short_args && f.out[f.out.len - 1] == ` ` {
if use_short_args && f.out.last() == ` ` {
// v Remove space at tail of line
// f(a, b, c, \n
// f1: 0\n

View File

@ -2529,7 +2529,7 @@ fn (mut g Gen) asm_stmt(stmt ast.AsmStmt) {
}
// swap destionation and operands for att syntax
if template.args.len != 0 && !template.is_directive {
template.args.prepend(template.args[template.args.len - 1])
template.args.prepend(template.args.last())
template.args.delete(template.args.len - 1)
}

View File

@ -98,7 +98,7 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) {
g.error('method `${m.name}()` (no value) used as value', node.pos)
}
expand_strs := if node.args.len > 0 && m.params.len - 1 >= node.args.len {
arg := node.args[node.args.len - 1]
arg := node.args.last()
param := m.params[node.args.len]
arg.expr is ast.Ident && g.table.type_to_str(arg.typ) == '[]string'

View File

@ -848,7 +848,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
}
g.write('${dot}_object')
is_variadic := node.expected_arg_types.len > 0
&& node.expected_arg_types[node.expected_arg_types.len - 1].has_flag(.variadic)
&& node.expected_arg_types.last().has_flag(.variadic)
if node.args.len > 0 || is_variadic {
g.write(', ')
g.call_args(node)
@ -1202,7 +1202,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.write(')')
}
is_variadic := node.expected_arg_types.len > 0
&& node.expected_arg_types[node.expected_arg_types.len - 1].has_flag(.variadic)
&& node.expected_arg_types.last().has_flag(.variadic)
if node.args.len > 0 || is_variadic {
g.write(', ')
}
@ -1784,8 +1784,8 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
// Handle `foo(c'str')` for `fn foo(args ...&u8)`
// TODOC2V handle this in a better place
g.expr(args[0].expr)
} else if args.len > 0 && args[args.len - 1].expr is ast.ArrayDecompose {
g.expr(args[args.len - 1].expr)
} else if args.len > 0 && args.last().expr is ast.ArrayDecompose {
g.expr(args.last().expr)
} else {
if variadic_count > 0 {
if g.pref.translated || g.file.is_translated {

View File

@ -113,7 +113,7 @@ pub fn (mut f Gen) wrap_long_line(penalty_idx int, add_indent bool) bool {
if f.buffering {
return false
}
if f.out[f.out.len - 1] == ` ` {
if f.out.last() == ` ` {
f.out.go_back(1)
}
f.write('\n')
@ -220,7 +220,7 @@ pub fn (mut f Gen) short_module(name string) string {
}
idx := vals.len - 1
mname, tprefix := f.get_modname_prefix(vals[..idx].join('.'))
symname := vals[vals.len - 1]
symname := vals.last()
mut aname := f.mod2alias[mname]
if aname == '' {
for _, v in f.mod2alias {

View File

@ -166,7 +166,7 @@ pub fn (mut f Gen) struct_init(node ast.StructInit) {
// fields_start := f.out.len
fields_loop: for {
if !single_line_fields {
if use_short_args && f.out[f.out.len - 1] == ` ` {
if use_short_args && f.out.last() == ` ` {
// v Remove space at tail of line
// f(a, b, c, \n
// f1: 0\n

View File

@ -284,7 +284,7 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) string {
/*
if pref.is_shared {
// Export, through CommonJS, the module of the entry file if `-shared` was passed
export := nodes[nodes.len - 1].name
export := nodes.last().name
out += 'if (typeof module === "object" && module.exports) module.exports = $export;\n'
}*/
out += '\n'
@ -3584,7 +3584,7 @@ fn (mut g JsGen) gen_type_cast_expr(it ast.CastExpr) {
return
}
if g.cast_stack.len > 0 && is_literal {
if it.typ == g.cast_stack[g.cast_stack.len - 1] {
if it.typ == g.cast_stack.last() {
g.expr(it.expr)
return
}
@ -3619,7 +3619,7 @@ fn (mut g JsGen) gen_integer_literal_expr(it ast.IntegerLiteral) {
// Don't wrap integers for use in JS.foo functions.
// TODO: call.language always seems to be "v", parser bug?
if g.call_stack.len > 0 {
call := g.call_stack[g.call_stack.len - 1]
call := g.call_stack.last()
if call.language == .js {
for t in call.args {
if t.expr is ast.IntegerLiteral {
@ -3634,7 +3634,7 @@ fn (mut g JsGen) gen_integer_literal_expr(it ast.IntegerLiteral) {
// Skip cast if type is the same as the parrent caster
if g.cast_stack.len > 0 {
if g.cast_stack[g.cast_stack.len - 1] in ast.integer_type_idxs {
if g.cast_stack.last() in ast.integer_type_idxs {
g.write('new ')
g.write('int($it.val)')
@ -3652,7 +3652,7 @@ fn (mut g JsGen) gen_float_literal_expr(it ast.FloatLiteral) {
// Don't wrap integers for use in JS.foo functions.
// TODO: call.language always seems to be "v", parser bug?
if g.call_stack.len > 0 {
call := g.call_stack[g.call_stack.len - 1]
call := g.call_stack.last()
if call.language == .js {
for i, t in call.args {
if t.expr is ast.FloatLiteral {
@ -3671,10 +3671,10 @@ fn (mut g JsGen) gen_float_literal_expr(it ast.FloatLiteral) {
// Skip cast if type is the same as the parrent caster
if g.cast_stack.len > 0 {
if g.cast_stack[g.cast_stack.len - 1] in ast.float_type_idxs {
if g.cast_stack.last() in ast.float_type_idxs {
g.write('new f32($it.val)')
return
} else if g.cast_stack[g.cast_stack.len - 1] in ast.integer_type_idxs {
} else if g.cast_stack.last() in ast.integer_type_idxs {
g.write(int(it.val.f64()).str())
return
}

View File

@ -310,7 +310,7 @@ pub fn (mut p Parser) parse_inline_sum_type() ast.Type {
variants := p.parse_sum_type_variants()
if variants.len > 1 {
if variants.len > parser.maximum_inline_sum_type_variants {
pos := variants[0].pos.extend(variants[variants.len - 1].pos)
pos := variants[0].pos.extend(variants.last().pos)
p.warn_with_pos('an inline sum type expects a maximum of $parser.maximum_inline_sum_type_variants types ($variants.len were given)',
pos)
}

View File

@ -106,7 +106,7 @@ pub fn short_module_name(name string) string {
return name
}
mname := vals[vals.len - 2]
symname := vals[vals.len - 1]
symname := vals.last()
return '${mname}.$symname'
}