fmt: prevent rare code corruption with array pre comments (#9908)

pull/9933/head
Lukas Neubert 2021-04-29 07:28:44 +02:00 committed by GitHub
parent dee733aae4
commit e711e8634e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 2 deletions

View File

@ -38,7 +38,6 @@ const (
'vlib/v/tests/string_interpolation_test.v' /* TODO byteptr: &byte.str() behaves differently than byteptr.str() */,
'vlib/v/gen/js/tests/js.v', /* local `hello` fn, gets replaced with module `hello` aliased as `hl` */
'examples/c_interop_wkhtmltopdf.v', /* &charptr --> &&char */
'examples/path_tracing.v', /* block --> line comments corrupts code */
'vlib/v/gen/c/cheaders.v' /* infix wrapping error */,
]
vfmt_verify_list = [

View File

@ -1428,7 +1428,12 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
f.write(' ')
}
} else {
if c.pos.last_line < node.pos.last_line {
next_line := if node.exprs.len > 0 {
node.exprs[0].position().line_nr
} else {
node.pos.last_line
}
if c.pos.last_line < next_line {
f.comment(c, level: .indent)
if node.exprs.len == 0 {
f.writeln('')

View File

@ -73,4 +73,7 @@ fn comment_at_line_start_with_expressions_after() {
arr := [123456789012345, 234567890123456, 678901234567890, 789012345678901, /* at the end */
345678901234567, /* in between */ 456789012345678,
/* line start */ 567890123456789, 890123456789012]
arr2 := [/* same line pre comment */ Foo{
a: 123
}]
}