checker: validate loop var names (#5677)

pull/5681/head
yuyi 2020-07-05 21:27:37 +08:00 committed by GitHub
parent cda9240632
commit 34a24eaa4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 7 deletions

View File

@ -27,8 +27,8 @@ pub fn (mut w Writer) write(record []string) ?bool {
return err_invalid_delim
}
le := if w.use_crlf { '\r\n' } else { '\n' }
for n, _field in record {
mut field := _field
for n, field_ in record {
mut field := field_
if n > 0 {
w.sb.write(w.delimiter.str())
}

View File

@ -153,8 +153,8 @@ pub fn read_cookies(h map[string][]string, filter string) []&Cookie {
return []
}
mut cookies := []&Cookie{}
for _, _line in lines {
mut line := _line.trim_space()
for _, line_ in lines {
mut line := line_.trim_space()
mut part := ''
for line.len > 0 {
if line.index_any(';') > 0 {

View File

@ -156,9 +156,9 @@ pub fn get_text(url string) string {
pub fn url_encode_form_data(data map[string]string) string {
mut pieces := []string{}
for _key, _value in data {
key := urllib.query_escape(_key)
value := urllib.query_escape(_value)
for key_, value_ in data {
key := urllib.query_escape(key_)
value := urllib.query_escape(value_)
pieces << '$key=$value'
}
return pieces.join('&')

View File

@ -1904,6 +1904,12 @@ fn (mut c Checker) stmt(node ast.Stmt) {
c.in_for_count++
typ := c.expr(node.cond)
typ_idx := typ.idx()
if node.key_var.len > 0 && node.key_var != '_' {
c.check_valid_snake_case(node.key_var, 'variable name', node.pos)
}
if node.val_var.len > 0 && node.val_var != '_' {
c.check_valid_snake_case(node.val_var, 'variable name', node.pos)
}
if node.is_range {
high_type := c.expr(node.high)
high_type_idx := high_type.idx()

View File

@ -0,0 +1,8 @@
vlib/v/checker/tests/incorrect_for_in_name_variable.v:3:6: error: variable name `_aa` cannot start with `_`
1 | fn main() {
2 | a := [1,2,3]
3 | for _aa in a {
| ~~~
4 | println(_aa)
5 | }

View File

@ -0,0 +1,6 @@
fn main() {
a := [1,2,3]
for _aa in a {
println(_aa)
}
}