From e3d328a92b0f592bb2c9d46ab5ae77a69ea03105 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 3 Dec 2025 10:18:35 +0200 Subject: [PATCH] v.parser: improve the unused import warning message (add fixing alternatives) (#25880) --- vlib/v/checker/tests/decompose_interface_err.out | 2 +- vlib/v/checker/tests/fn_param_import_sym_conflict.out | 6 +++--- vlib/v/checker/tests/import_symbol_fn_err.out | 2 +- vlib/v/checker/tests/import_unused_warning.out | 2 +- vlib/v/checker/tests/incorrect_name_module.out | 2 +- vlib/v/checker/tests/os_prefix.out | 2 +- vlib/v/checker/tests/selective_const_import.out | 2 +- vlib/v/checker/tests/unused_import_err.out | 4 ++-- vlib/v/checker/tests/var_decl_import_sym_conflict.out | 2 +- vlib/v/parser/module.v | 3 ++- vlib/v/parser/tests/import_sym_fn_unused_warning_err.out | 2 +- vlib/v/tests/modules/sub/foo.c.v | 7 +------ vlib/v/tests/modules/sub/foo/c/foo.v | 2 -- 13 files changed, 16 insertions(+), 22 deletions(-) diff --git a/vlib/v/checker/tests/decompose_interface_err.out b/vlib/v/checker/tests/decompose_interface_err.out index c5feaa7e7..80786680f 100644 --- a/vlib/v/checker/tests/decompose_interface_err.out +++ b/vlib/v/checker/tests/decompose_interface_err.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/decompose_interface_err.vv:3:8: warning: module 'arrays' is imported but never used +vlib/v/checker/tests/decompose_interface_err.vv:3:8: warning: module 'arrays' is imported but never used. Use `import arrays as _`, to silence this warning, or just remove the unused import line 1 | module main 2 | 3 | import arrays diff --git a/vlib/v/checker/tests/fn_param_import_sym_conflict.out b/vlib/v/checker/tests/fn_param_import_sym_conflict.out index 355ee576a..7aea811b1 100644 --- a/vlib/v/checker/tests/fn_param_import_sym_conflict.out +++ b/vlib/v/checker/tests/fn_param_import_sym_conflict.out @@ -1,15 +1,15 @@ -vlib/v/checker/tests/fn_param_import_sym_conflict.vv:1:8: warning: module 'arrays' is imported but never used +vlib/v/checker/tests/fn_param_import_sym_conflict.vv:1:8: warning: module 'arrays' is imported but never used. Use `import arrays as _`, to silence this warning, or just remove the unused import line 1 | import arrays | ~~~~~~ 2 | import maps 3 | import strings as strs -vlib/v/checker/tests/fn_param_import_sym_conflict.vv:2:8: warning: module 'maps' is imported but never used +vlib/v/checker/tests/fn_param_import_sym_conflict.vv:2:8: warning: module 'maps' is imported but never used. Use `import maps as _`, to silence this warning, or just remove the unused import line 1 | import arrays 2 | import maps | ~~~~ 3 | import strings as strs 4 | -vlib/v/checker/tests/fn_param_import_sym_conflict.vv:3:8: warning: module 'strs (strings)' is imported but never used +vlib/v/checker/tests/fn_param_import_sym_conflict.vv:3:8: warning: module 'strs (strings)' is imported but never used. Use `import strs (strings) as _`, to silence this warning, or just remove the unused import line 1 | import arrays 2 | import maps 3 | import strings as strs diff --git a/vlib/v/checker/tests/import_symbol_fn_err.out b/vlib/v/checker/tests/import_symbol_fn_err.out index 4148cd8f8..60c193556 100644 --- a/vlib/v/checker/tests/import_symbol_fn_err.out +++ b/vlib/v/checker/tests/import_symbol_fn_err.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/import_symbol_fn_err.vv:1:8: warning: module 'crypto' is imported but never used +vlib/v/checker/tests/import_symbol_fn_err.vv:1:8: warning: module 'crypto' is imported but never used. Use `import crypto as _`, to silence this warning, or just remove the unused import line 1 | import crypto { userper } | ~~~~~~ 2 | fn main() { diff --git a/vlib/v/checker/tests/import_unused_warning.out b/vlib/v/checker/tests/import_unused_warning.out index a05b57851..8e2e38a80 100644 --- a/vlib/v/checker/tests/import_unused_warning.out +++ b/vlib/v/checker/tests/import_unused_warning.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/import_unused_warning.vv:2:8: warning: module 'time' is imported but never used +vlib/v/checker/tests/import_unused_warning.vv:2:8: warning: module 'time' is imported but never used. Use `import time as _`, to silence this warning, or just remove the unused import line 1 | import os as _ 2 | import time | ~~~~ diff --git a/vlib/v/checker/tests/incorrect_name_module.out b/vlib/v/checker/tests/incorrect_name_module.out index 1ca2c466b..c3a99eaf4 100644 --- a/vlib/v/checker/tests/incorrect_name_module.out +++ b/vlib/v/checker/tests/incorrect_name_module.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/incorrect_name_module.vv:4:8: warning: module 'os' is imported but never used +vlib/v/checker/tests/incorrect_name_module.vv:4:8: warning: module 'os' is imported but never used. Use `import os as _`, to silence this warning, or just remove the unused import line 2 | 3 | import math as _ 4 | import os diff --git a/vlib/v/checker/tests/os_prefix.out b/vlib/v/checker/tests/os_prefix.out index e047c9769..05fd6de95 100644 --- a/vlib/v/checker/tests/os_prefix.out +++ b/vlib/v/checker/tests/os_prefix.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/os_prefix.vv:1:8: warning: module 'os' is imported but never used +vlib/v/checker/tests/os_prefix.vv:1:8: warning: module 'os' is imported but never used. Use `import os as _`, to silence this warning, or just remove the unused import line 1 | import os | ~~ 2 | diff --git a/vlib/v/checker/tests/selective_const_import.out b/vlib/v/checker/tests/selective_const_import.out index e269f6dc2..ef1d03e8a 100644 --- a/vlib/v/checker/tests/selective_const_import.out +++ b/vlib/v/checker/tests/selective_const_import.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/selective_const_import.vv:1:8: warning: module 'time' is imported but never used +vlib/v/checker/tests/selective_const_import.vv:1:8: warning: module 'time' is imported but never used. Use `import time as _`, to silence this warning, or just remove the unused import line 1 | import time { second } | ~~~~ vlib/v/checker/tests/selective_const_import.vv:1:15: error: cannot selectively import constant `second` from `time`, import `time` and use `time.second` instead diff --git a/vlib/v/checker/tests/unused_import_err.out b/vlib/v/checker/tests/unused_import_err.out index 99d4792eb..f29c1c71f 100644 --- a/vlib/v/checker/tests/unused_import_err.out +++ b/vlib/v/checker/tests/unused_import_err.out @@ -1,8 +1,8 @@ -vlib/v/checker/tests/unused_import_err.vv:1:8: warning: module 'te (term)' is imported but never used +vlib/v/checker/tests/unused_import_err.vv:1:8: warning: module 'te (term)' is imported but never used. Use `import te (term) as _`, to silence this warning, or just remove the unused import line 1 | import term as te | ~~~~ 2 | import te.ui -vlib/v/checker/tests/unused_import_err.vv:2:8: warning: module 'ui (te.ui)' is imported but never used +vlib/v/checker/tests/unused_import_err.vv:2:8: warning: module 'ui (te.ui)' is imported but never used. Use `import ui (te.ui) as _`, to silence this warning, or just remove the unused import line 1 | import term as te 2 | import te.ui | ~~~~~ diff --git a/vlib/v/checker/tests/var_decl_import_sym_conflict.out b/vlib/v/checker/tests/var_decl_import_sym_conflict.out index 4f8a6aed6..049cff61e 100644 --- a/vlib/v/checker/tests/var_decl_import_sym_conflict.out +++ b/vlib/v/checker/tests/var_decl_import_sym_conflict.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/var_decl_import_sym_conflict.vv:1:8: warning: module 'arrays' is imported but never used +vlib/v/checker/tests/var_decl_import_sym_conflict.vv:1:8: warning: module 'arrays' is imported but never used. Use `import arrays as _`, to silence this warning, or just remove the unused import line 1 | import arrays | ~~~~~~ 2 | diff --git a/vlib/v/parser/module.v b/vlib/v/parser/module.v index a9eb51169..0d34ed006 100644 --- a/vlib/v/parser/module.v +++ b/vlib/v/parser/module.v @@ -89,7 +89,8 @@ fn (mut p Parser) check_unused_imports() { if !(alias.len == 1 && alias[0] == `_`) && !p.is_used_import(alias) && alias !in p.auto_imports { mod_alias := if alias == mod { alias } else { '${alias} (${mod})' } - p.warn_with_pos("module '${mod_alias}' is imported but never used", import_m.mod_pos) + p.warn_with_pos("module '${mod_alias}' is imported but never used. Use `import ${mod_alias} as _`, to silence this warning, or just remove the unused import line", + import_m.mod_pos) } } } diff --git a/vlib/v/parser/tests/import_sym_fn_unused_warning_err.out b/vlib/v/parser/tests/import_sym_fn_unused_warning_err.out index 9b502e531..622bcc4a4 100644 --- a/vlib/v/parser/tests/import_sym_fn_unused_warning_err.out +++ b/vlib/v/parser/tests/import_sym_fn_unused_warning_err.out @@ -1,4 +1,4 @@ -vlib/v/parser/tests/import_sym_fn_unused_warning_err.vv:1:8: warning: module 'math' is imported but never used +vlib/v/parser/tests/import_sym_fn_unused_warning_err.vv:1:8: warning: module 'math' is imported but never used. Use `import math as _`, to silence this warning, or just remove the unused import line 1 | import math { sin, cos } | ~~~~ 2 | diff --git a/vlib/v/tests/modules/sub/foo.c.v b/vlib/v/tests/modules/sub/foo.c.v index c133a1991..b031777c8 100644 --- a/vlib/v/tests/modules/sub/foo.c.v +++ b/vlib/v/tests/modules/sub/foo.c.v @@ -1,6 +1,6 @@ module sub -import sub.foo.c +import sub.foo.c as _ // imported only for the C declarations @[typedef] pub struct C.sub_foo { @@ -8,8 +8,3 @@ pub struct C.sub_foo { } pub type Foo = C.sub_foo - -// avoiding compiler warnings: module 'c (sub.foo.c)' is imported but never used -fn bar() { - _ = c.used_import -} diff --git a/vlib/v/tests/modules/sub/foo/c/foo.v b/vlib/v/tests/modules/sub/foo/c/foo.v index 199d3d13a..0af496f30 100644 --- a/vlib/v/tests/modules/sub/foo/c/foo.v +++ b/vlib/v/tests/modules/sub/foo/c/foo.v @@ -1,7 +1,5 @@ module c -pub const used_import = 1 - #flag -I @VMODROOT #include "foo.h" -- 2.39.5