mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
compiler: blank identifier for loops. dont declare any variables
This commit is contained in:
parent
7952d591b9
commit
ed3a4961d0
@ -543,11 +543,10 @@ fn (p mut Parser) check_unused_variables() {
|
||||
if var.name == '' {
|
||||
break
|
||||
}
|
||||
if !var.is_used && !p.pref.is_repl && !var.is_arg && !p.pref.translated && var.name != '_' {
|
||||
if !var.is_used && !p.pref.is_repl && !var.is_arg && !p.pref.translated {
|
||||
p.production_error('`$var.name` declared and not used', var.scanner_pos )
|
||||
}
|
||||
if !var.is_changed && var.is_mut && !p.pref.is_repl &&
|
||||
!p.pref.translated && var.name != '_' {
|
||||
if !var.is_changed && var.is_mut && !p.pref.is_repl && !p.pref.translated {
|
||||
p.error_with_position( '`$var.name` is declared as mutable, but it was never changed', var.scanner_pos )
|
||||
}
|
||||
}
|
||||
|
@ -256,17 +256,20 @@ fn (p mut Parser) gen_array_at(typ_ string, is_arr0 bool, fn_ph int) {
|
||||
|
||||
fn (p mut Parser) gen_for_header(i, tmp, var_typ, val string) {
|
||||
p.genln('for (int $i = 0; $i < ${tmp}.len; $i++) {')
|
||||
if val == '_' { return }
|
||||
p.genln('$var_typ $val = (($var_typ *) $tmp . data)[$i];')
|
||||
}
|
||||
|
||||
fn (p mut Parser) gen_for_str_header(i, tmp, var_typ, val string) {
|
||||
p.genln('array_byte bytes_$tmp = string_bytes( $tmp );')
|
||||
p.genln(';\nfor (int $i = 0; $i < $tmp .len; $i ++) {')
|
||||
if val == '_' { return }
|
||||
p.genln('$var_typ $val = (($var_typ *) bytes_$tmp . data)[$i];')
|
||||
}
|
||||
|
||||
fn (p mut Parser) gen_for_range_header(i, range_end, tmp, var_type, val string) {
|
||||
p.genln(';\nfor (int $i = $tmp; $i < $range_end; $i++) {')
|
||||
if val == '_' { return }
|
||||
p.genln('$var_type $val = $i;')
|
||||
}
|
||||
|
||||
@ -277,6 +280,7 @@ fn (p mut Parser) gen_for_map_header(i, tmp, var_typ, val, typ string) {
|
||||
p.genln('string $i = ((string*)keys_$tmp .data)[l];')
|
||||
// TODO don't call map_get() for each key, fetch values while traversing
|
||||
// the tree (replace `map_keys()` above with `map_key_vals()`)
|
||||
if val == '_' { return }
|
||||
p.genln('$var_typ $val = $def; map_get($tmp, $i, & $val);')
|
||||
}
|
||||
|
||||
|
@ -99,21 +99,25 @@ fn (p mut Parser) gen_array_at(typ string, is_arr0 bool, fn_ph int) {
|
||||
|
||||
fn (p mut Parser) gen_for_header(i, tmp, var_typ, val string) {
|
||||
p.genln('for (var $i = 0; $i < ${tmp}.length; $i++) {')
|
||||
if val == '_' { return }
|
||||
p.genln('var $val = $tmp [$i];')
|
||||
}
|
||||
|
||||
fn (p mut Parser) gen_for_range_header(i, range_end, tmp, var_type, val string) {
|
||||
p.genln(';\nfor (var $i = $tmp; $i < $range_end; $i++) {')
|
||||
if val == '_' { return }
|
||||
p.genln('var /*$var_type*/ $val = $i;')
|
||||
}
|
||||
|
||||
fn (p mut Parser) gen_for_str_header(i, tmp, var_typ, val string) {
|
||||
p.genln('for (var $i = 0; $i < $tmp .length; $i ++) {')
|
||||
if val == '_' { return }
|
||||
p.genln('var $val = $tmp[$i];')
|
||||
}
|
||||
|
||||
fn (p mut Parser) gen_for_map_header(i, tmp, var_typ, val, typ string) {
|
||||
p.genln('for (var $i in $tmp) {')
|
||||
if val == '_' { return }
|
||||
p.genln('var $val = $tmp[$i];')
|
||||
}
|
||||
|
||||
|
@ -3275,44 +3275,33 @@ fn (p mut Parser) for_st() {
|
||||
pad := if is_arr { 6 } else { 4 }
|
||||
var_typ := if is_str { 'byte' } else { typ.right(pad) }
|
||||
// typ = strings.Replace(typ, "_ptr", "*", -1)
|
||||
// Register temp var
|
||||
val_var := Var {
|
||||
name: val
|
||||
typ: var_typ
|
||||
ptr: typ.contains('*')
|
||||
}
|
||||
p.register_var(val_var)
|
||||
mut i_var_type := 'int'
|
||||
if is_arr {
|
||||
i_var := Var {
|
||||
name: i
|
||||
typ: 'int'
|
||||
// parent_fn: p.cur_fn
|
||||
is_mut: true
|
||||
is_changed: true
|
||||
}
|
||||
//p.genln(';\nfor ($i_type $i = 0; $i < $tmp .len; $i ++) {')
|
||||
p.gen_for_header(i, tmp, var_typ, val)
|
||||
p.register_var(i_var)
|
||||
}
|
||||
else if is_map {
|
||||
i_var := Var {
|
||||
name: i
|
||||
typ: 'string'
|
||||
is_mut: true
|
||||
is_changed: true
|
||||
}
|
||||
p.register_var(i_var)
|
||||
i_var_type = 'string'
|
||||
p.gen_for_map_header(i, tmp, var_typ, val, typ)
|
||||
}
|
||||
else if is_str {
|
||||
i_var := Var {
|
||||
i_var_type = 'byte'
|
||||
p.gen_for_str_header(i, tmp, var_typ, val)
|
||||
}
|
||||
// Register temp vars
|
||||
if i != '_' {
|
||||
p.register_var(Var {
|
||||
name: i
|
||||
typ: 'byte'
|
||||
typ: i_var_type
|
||||
is_mut: true
|
||||
is_changed: true
|
||||
}
|
||||
p.register_var(i_var)
|
||||
p.gen_for_str_header(i, tmp, var_typ, val)
|
||||
})
|
||||
}
|
||||
if val != '_' {
|
||||
p.register_var(Var {
|
||||
name: val
|
||||
typ: var_typ
|
||||
ptr: typ.contains('*')
|
||||
})
|
||||
}
|
||||
}
|
||||
// `for val in vals`
|
||||
@ -3357,13 +3346,14 @@ fn (p mut Parser) for_st() {
|
||||
}
|
||||
// println('for typ=$typ vartyp=$var_typ')
|
||||
// Register temp var
|
||||
val_var := Var {
|
||||
name: val
|
||||
typ: var_type
|
||||
ptr: typ.contains('*')
|
||||
is_changed: true
|
||||
if val != '_' {
|
||||
p.register_var(Var {
|
||||
name: val
|
||||
typ: var_type
|
||||
ptr: typ.contains('*')
|
||||
is_changed: true
|
||||
})
|
||||
}
|
||||
p.register_var(val_var)
|
||||
i := p.get_tmp()
|
||||
if is_arr {
|
||||
p.gen_for_header(i, tmp, var_type, val)
|
||||
@ -3374,7 +3364,7 @@ fn (p mut Parser) for_st() {
|
||||
else if is_range {
|
||||
p.gen_for_range_header(i, range_end, tmp, var_type, val)
|
||||
}
|
||||
} else {
|
||||
} else {
|
||||
// `for a < b {`
|
||||
p.gen('while (')
|
||||
p.check_types(p.bool_expression(), 'bool')
|
||||
|
Loading…
Reference in New Issue
Block a user