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 return
} }
stmts_len := node.stmts.len
if stmts_len == 0 { if node.stmts.len == 0 {
if ret_type != ast.void_type { if ret_type != ast.void_type {
// x := f() or {} // x := f() or {}
c.error('assignment requires a non empty `or {}` block', node.pos) 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 {}` // allow `f() or {}`
return 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)) 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 { } else {
if signed { if signed {
if iseen.len > 0 { if iseen.len > 0 {
ilast := iseen[iseen.len - 1] ilast := iseen.last()
if ilast == enum_imax { if ilast == enum_imax {
c.error('enum value overflows type `$senum_type`, which has a maximum value of $enum_imax', c.error('enum value overflows type `$senum_type`, which has a maximum value of $enum_imax',
field.pos) field.pos)
@ -1551,7 +1551,7 @@ pub fn (mut c Checker) enum_decl(mut node ast.EnumDecl) {
} }
} else { } else {
if useen.len > 0 { if useen.len > 0 {
ulast := useen[useen.len - 1] ulast := useen.last()
if ulast == enum_umax { if ulast == enum_umax {
c.error('enum value overflows type `$senum_type`, which has a maximum value of $enum_umax', c.error('enum value overflows type `$senum_type`, which has a maximum value of $enum_umax',
field.pos) 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 { param := if func.is_variadic && i >= func.params.len - 1 {
func.params[func.params.len - 1] func.params.last()
} else { } else {
func.params[i] 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 { if func.generic_names.len > 0 {
for i, mut call_arg in node.args { for i, mut call_arg in node.args {
param := if func.is_variadic && i >= func.params.len - 1 { param := if func.is_variadic && i >= func.params.len - 1 {
func.params[func.params.len - 1] func.params.last()
} else { } else {
func.params[i] 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 mut has_else := else_branch.is_else
if !has_else { if !has_else {
for i, branch in node.branches { 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 { pub fn (graph &DepGraph) last_node() DepGraphNode {
return graph.nodes[graph.nodes.len - 1] return graph.nodes.last()
} }
pub fn (graph &DepGraph) display() string { 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] { if penalty_idx > 0 && f.line_len <= fmt.max_len[penalty_idx] {
return false return false
} }
if f.out[f.out.len - 1] == ` ` { if f.out.last() == ` ` {
f.out.go_back(1) f.out.go_back(1)
} }
f.write('\n') f.write('\n')
@ -255,7 +255,7 @@ pub fn (mut f Fmt) short_module(name string) string {
} }
idx := vals.len - 1 idx := vals.len - 1
mname, tprefix := f.get_modname_prefix(vals[..idx].join('.')) mname, tprefix := f.get_modname_prefix(vals[..idx].join('.'))
symname := vals[vals.len - 1] symname := vals.last()
mut aname := f.mod2alias[mname] mut aname := f.mod2alias[mname]
if aname == '' { if aname == '' {
for _, v in f.mod2alias { for _, v in f.mod2alias {
@ -1955,7 +1955,7 @@ pub fn (mut f Fmt) ident(node ast.Ident) {
// "v.fmt.foo" => "fmt.foo" // "v.fmt.foo" => "fmt.foo"
vals := full_name.split('.') vals := full_name.split('.')
mod_prefix := vals[vals.len - 2] mod_prefix := vals[vals.len - 2]
const_name := vals[vals.len - 1] const_name := vals.last()
if mod_prefix == 'main' { if mod_prefix == 'main' {
f.write(const_name) f.write(const_name)
return return

View File

@ -264,7 +264,7 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
fields_start := f.out.len fields_start := f.out.len
fields_loop: for { fields_loop: for {
if !single_line_fields { 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 // v Remove space at tail of line
// f(a, b, c, \n // f(a, b, c, \n
// f1: 0\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 // swap destionation and operands for att syntax
if template.args.len != 0 && !template.is_directive { 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) 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) 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 { 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] param := m.params[node.args.len]
arg.expr is ast.Ident && g.table.type_to_str(arg.typ) == '[]string' 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') g.write('${dot}_object')
is_variadic := node.expected_arg_types.len > 0 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 { if node.args.len > 0 || is_variadic {
g.write(', ') g.write(', ')
g.call_args(node) g.call_args(node)
@ -1202,7 +1202,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.write(')') g.write(')')
} }
is_variadic := node.expected_arg_types.len > 0 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 { if node.args.len > 0 || is_variadic {
g.write(', ') g.write(', ')
} }
@ -1784,8 +1784,8 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
// Handle `foo(c'str')` for `fn foo(args ...&u8)` // Handle `foo(c'str')` for `fn foo(args ...&u8)`
// TODOC2V handle this in a better place // TODOC2V handle this in a better place
g.expr(args[0].expr) g.expr(args[0].expr)
} else if args.len > 0 && args[args.len - 1].expr is ast.ArrayDecompose { } else if args.len > 0 && args.last().expr is ast.ArrayDecompose {
g.expr(args[args.len - 1].expr) g.expr(args.last().expr)
} else { } else {
if variadic_count > 0 { if variadic_count > 0 {
if g.pref.translated || g.file.is_translated { 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 { if f.buffering {
return false return false
} }
if f.out[f.out.len - 1] == ` ` { if f.out.last() == ` ` {
f.out.go_back(1) f.out.go_back(1)
} }
f.write('\n') f.write('\n')
@ -220,7 +220,7 @@ pub fn (mut f Gen) short_module(name string) string {
} }
idx := vals.len - 1 idx := vals.len - 1
mname, tprefix := f.get_modname_prefix(vals[..idx].join('.')) mname, tprefix := f.get_modname_prefix(vals[..idx].join('.'))
symname := vals[vals.len - 1] symname := vals.last()
mut aname := f.mod2alias[mname] mut aname := f.mod2alias[mname]
if aname == '' { if aname == '' {
for _, v in f.mod2alias { 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_start := f.out.len
fields_loop: for { fields_loop: for {
if !single_line_fields { 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 // v Remove space at tail of line
// f(a, b, c, \n // f(a, b, c, \n
// f1: 0\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 { if pref.is_shared {
// Export, through CommonJS, the module of the entry file if `-shared` was passed // 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 += 'if (typeof module === "object" && module.exports) module.exports = $export;\n'
}*/ }*/
out += '\n' out += '\n'
@ -3584,7 +3584,7 @@ fn (mut g JsGen) gen_type_cast_expr(it ast.CastExpr) {
return return
} }
if g.cast_stack.len > 0 && is_literal { 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) g.expr(it.expr)
return 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. // Don't wrap integers for use in JS.foo functions.
// TODO: call.language always seems to be "v", parser bug? // TODO: call.language always seems to be "v", parser bug?
if g.call_stack.len > 0 { if g.call_stack.len > 0 {
call := g.call_stack[g.call_stack.len - 1] call := g.call_stack.last()
if call.language == .js { if call.language == .js {
for t in call.args { for t in call.args {
if t.expr is ast.IntegerLiteral { 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 // Skip cast if type is the same as the parrent caster
if g.cast_stack.len > 0 { 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('new ')
g.write('int($it.val)') 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. // Don't wrap integers for use in JS.foo functions.
// TODO: call.language always seems to be "v", parser bug? // TODO: call.language always seems to be "v", parser bug?
if g.call_stack.len > 0 { if g.call_stack.len > 0 {
call := g.call_stack[g.call_stack.len - 1] call := g.call_stack.last()
if call.language == .js { if call.language == .js {
for i, t in call.args { for i, t in call.args {
if t.expr is ast.FloatLiteral { 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 // Skip cast if type is the same as the parrent caster
if g.cast_stack.len > 0 { 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)') g.write('new f32($it.val)')
return 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()) g.write(int(it.val.f64()).str())
return return
} }

View File

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

View File

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