From 50ab2cfd1ae02d4f4280f38c60b8dbd17f7599f6 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 21 May 2022 20:01:58 +0800 Subject: [PATCH] fmt: fix 'strings' name variable call generate auto import (fix #9713) (#14485) --- vlib/v/fmt/fmt.v | 3 ++- vlib/v/fmt/tests/strings_name_variable_call_keep.vv | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 vlib/v/fmt/tests/strings_name_variable_call_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 8bd1e794ee..470c077ea3 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1651,7 +1651,8 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) { // `time.now()` without `time imported` is processed as a method call with `time` being // a `node.left` expression. Import `time` automatically. // TODO fetch all available modules - if node.left.name in ['time', 'os', 'strings', 'math', 'json', 'base64'] { + if node.left.name in ['time', 'os', 'strings', 'math', 'json', 'base64'] + && !node.left.scope.known_var(node.left.name) { f.file.imports << ast.Import{ mod: node.left.name alias: node.left.name diff --git a/vlib/v/fmt/tests/strings_name_variable_call_keep.vv b/vlib/v/fmt/tests/strings_name_variable_call_keep.vv new file mode 100644 index 0000000000..5e9abcfa43 --- /dev/null +++ b/vlib/v/fmt/tests/strings_name_variable_call_keep.vv @@ -0,0 +1,5 @@ +fn main() { + strings := 'hello' + a := strings.repeat(2) + println(a) +}