From 72c7c4e3a3b06c3f3b5e550d40f5340a4e0ea5db Mon Sep 17 00:00:00 2001 From: Swastik <swstkbaranwal@gmail.com> Date: Thu, 2 Nov 2023 19:50:18 +0530 Subject: [PATCH 1/3] checker: warn on unused imported functions used via `import math { sin, cos }` --- vlib/v/checker/checker.v | 3 +++ .../checker/tests/import_sym_fn_unused_warning_err.out | 10 ++++++++++ .../checker/tests/import_sym_fn_unused_warning_err.vv | 3 +++ 3 files changed, 16 insertions(+) create mode 100644 vlib/v/checker/tests/import_sym_fn_unused_warning_err.out create mode 100644 vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7a7f9d5cf36fa6..5a45594f6181bd 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2794,6 +2794,9 @@ fn (mut c Checker) import_stmt(node ast.Import) { if !func.is_pub { c.error('module `${node.mod}` function `${sym.name}()` is private', sym.pos) } + if func.usages != 1 { + c.warn('module `${node.mod}` function `${sym.name}()` is unused', sym.pos) + } continue } if _ := c.file.global_scope.find_const(name) { diff --git a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out new file mode 100644 index 00000000000000..d0bb2c363db71a --- /dev/null +++ b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out @@ -0,0 +1,10 @@ +vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv:1:15: warning: module `math` function `sin()` is unused + 1 | import math { sin, cos } + | ~~~ + 2 | + 3 | fn main() {} +vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv:1:20: warning: module `math` function `cos()` is unused + 1 | import math { sin, cos } + | ~~~ + 2 | + 3 | fn main() {} diff --git a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv new file mode 100644 index 00000000000000..e5a7d32933c895 --- /dev/null +++ b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv @@ -0,0 +1,3 @@ +import math { sin, cos } + +fn main() {} From dbd2e291052836c6f317b746bdb805a91dcac6c9 Mon Sep 17 00:00:00 2001 From: Swastik <swstkbaranwal@gmail.com> Date: Thu, 2 Nov 2023 19:50:42 +0530 Subject: [PATCH 2/3] Revert "checker: warn on unused imported functions used via `import math { sin, cos }`" This reverts commit ddf0468f1c4f9af54ada162712c212214e58ca56. --- vlib/v/checker/checker.v | 3 --- .../checker/tests/import_sym_fn_unused_warning_err.out | 10 ---------- .../checker/tests/import_sym_fn_unused_warning_err.vv | 3 --- 3 files changed, 16 deletions(-) delete mode 100644 vlib/v/checker/tests/import_sym_fn_unused_warning_err.out delete mode 100644 vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5a45594f6181bd..7a7f9d5cf36fa6 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2794,9 +2794,6 @@ fn (mut c Checker) import_stmt(node ast.Import) { if !func.is_pub { c.error('module `${node.mod}` function `${sym.name}()` is private', sym.pos) } - if func.usages != 1 { - c.warn('module `${node.mod}` function `${sym.name}()` is unused', sym.pos) - } continue } if _ := c.file.global_scope.find_const(name) { diff --git a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out deleted file mode 100644 index d0bb2c363db71a..00000000000000 --- a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out +++ /dev/null @@ -1,10 +0,0 @@ -vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv:1:15: warning: module `math` function `sin()` is unused - 1 | import math { sin, cos } - | ~~~ - 2 | - 3 | fn main() {} -vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv:1:20: warning: module `math` function `cos()` is unused - 1 | import math { sin, cos } - | ~~~ - 2 | - 3 | fn main() {} diff --git a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv deleted file mode 100644 index e5a7d32933c895..00000000000000 --- a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv +++ /dev/null @@ -1,3 +0,0 @@ -import math { sin, cos } - -fn main() {} From 7bed68cab8f7fec3fe5c9f6b901e0a4832c5a1d0 Mon Sep 17 00:00:00 2001 From: Swastik <swstkbaranwal@gmail.com> Date: Sun, 2 Feb 2025 00:26:56 +0530 Subject: [PATCH 3/3] parser: do not warn on imported sym as long as it is used --- .../tests/modules/enum_from_string_in_different_mods.out | 7 ------- vlib/v/parser/parser.v | 3 +++ vlib/v/slow_tests/inout/import_sym_field_no_warn.out | 1 + vlib/v/slow_tests/inout/import_sym_field_no_warn.vv | 7 +++++++ 4 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 vlib/v/slow_tests/inout/import_sym_field_no_warn.out create mode 100644 vlib/v/slow_tests/inout/import_sym_field_no_warn.vv diff --git a/vlib/v/checker/tests/modules/enum_from_string_in_different_mods.out b/vlib/v/checker/tests/modules/enum_from_string_in_different_mods.out index e597eb825383c3..9a5cc8986622d3 100644 --- a/vlib/v/checker/tests/modules/enum_from_string_in_different_mods.out +++ b/vlib/v/checker/tests/modules/enum_from_string_in_different_mods.out @@ -1,10 +1,3 @@ -vlib/v/checker/tests/modules/enum_from_string_in_different_mods/src/main.v:3:8: warning: module 'mod' is imported but never used - 1 | module main - 2 | - 3 | import mod { MyEnum, MyStruct } - | ~~~ - 4 | - 5 | fn main() { vlib/v/checker/tests/modules/enum_from_string_in_different_mods/src/main.v:3:14: error: module `mod` type `MyEnum` is private 1 | module main 2 | diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index d3d9b6553da895..0ddfb0e037bfea 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -747,6 +747,9 @@ fn (mut p Parser) check_name() string { name := p.tok.lit if p.tok.kind != .name && p.peek_tok.kind == .dot && name in p.imports { p.register_used_import(name) + } else if p.tok.kind == .name && p.peek_tok.kind == .dot && name in p.imported_symbols { + // symbols like Enum.field_name + p.register_used_import_for_symbol_name(p.imported_symbols[name]) } if !is_ident_name(name) { p.check(.name) diff --git a/vlib/v/slow_tests/inout/import_sym_field_no_warn.out b/vlib/v/slow_tests/inout/import_sym_field_no_warn.out new file mode 100644 index 00000000000000..09eb42eeed510c --- /dev/null +++ b/vlib/v/slow_tests/inout/import_sym_field_no_warn.out @@ -0,0 +1 @@ +same_site_not_set diff --git a/vlib/v/slow_tests/inout/import_sym_field_no_warn.vv b/vlib/v/slow_tests/inout/import_sym_field_no_warn.vv new file mode 100644 index 00000000000000..9250634bb3990c --- /dev/null +++ b/vlib/v/slow_tests/inout/import_sym_field_no_warn.vv @@ -0,0 +1,7 @@ +import net.http { SameSite } + +fn main() { + mime := SameSite.same_site_not_set + + println(mime) +}