checker: fix json.encode_pretty with a struct init expression argument (#22897)

This commit is contained in:
Turiiya 2024-11-18 09:42:34 +01:00 committed by GitHub
parent 47c785886c
commit 3bf459c0d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 2 deletions

View File

@ -461,6 +461,13 @@ fn test_omit_empty() {
// println(json.encode_pretty(foo))
}
fn test_encode_struct_expression() {
assert json.encode(Foo2{'Foo'}) == '{"name":"Foo"}'
assert json.encode_pretty(Foo2{'Bar'}) == '{
"name": "Bar"
}'
}
struct Asdasd {
data GamePacketData
}

View File

@ -1563,8 +1563,8 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
|| final_param_sym.idx == ast.voidptr_type_idx
|| param.typ == ast.nil_type || final_param_sym.idx == ast.nil_type_idx)
&& !call_arg.typ.is_any_kind_of_pointer() && func.language == .v
&& !call_arg.expr.is_lvalue() && func.name != 'json.encode' && !c.pref.translated
&& !c.file.is_translated {
&& !call_arg.expr.is_lvalue() && !c.pref.translated && !c.file.is_translated
&& func.name !in ['json.encode', 'json.encode_pretty'] {
c.error('expression cannot be passed as `voidptr`', call_arg.expr.pos())
}
// Handle expected interface