From 9f5f82b2b714e9b1bd5e1c3fed557283bff0b860 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 16 Sep 2023 23:14:00 +0300 Subject: [PATCH] all: do not warn/error for `import flag as _` --- vlib/v/checker/checker.v | 2 +- vlib/v/checker/tests/import_unused_warning.out | 9 +++++---- vlib/v/checker/tests/import_unused_warning.vv | 2 ++ vlib/v/checker/tests/incorrect_name_module.out | 9 ++++++++- vlib/v/checker/tests/incorrect_name_module.vv | 3 +++ .../modules/module_alias_started_with_underscore.out | 6 +++--- .../modules/module_alias_started_with_underscore/main.v | 4 ++-- .../unused_import_aliased_as_underscore.c.must_have | 3 +++ .../c/testdata/unused_import_aliased_as_underscore.vv | 1 + vlib/v/parser/module.v | 2 +- 10 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 vlib/v/gen/c/testdata/unused_import_aliased_as_underscore.c.must_have create mode 100644 vlib/v/gen/c/testdata/unused_import_aliased_as_underscore.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 4c491ca5d..b2b20b3ab 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -458,7 +458,7 @@ fn (mut c Checker) check_valid_snake_case(name string, identifier string, pos to if c.pref.translated || c.file.is_translated { return } - if !c.pref.is_vweb && name.len > 0 && (name[0] == `_` || name.contains('._')) { + if !c.pref.is_vweb && name.len > 1 && (name[0] == `_` || name.contains('._')) { c.error('${identifier} `${name}` cannot start with `_`', pos) } if !c.pref.experimental && util.contains_capital(name) { diff --git a/vlib/v/checker/tests/import_unused_warning.out b/vlib/v/checker/tests/import_unused_warning.out index 38df8cde2..a05b57851 100644 --- a/vlib/v/checker/tests/import_unused_warning.out +++ b/vlib/v/checker/tests/import_unused_warning.out @@ -1,5 +1,6 @@ -vlib/v/checker/tests/import_unused_warning.vv:1:8: warning: module 'time' is imported but never used - 1 | import time +vlib/v/checker/tests/import_unused_warning.vv:2:8: warning: module 'time' is imported but never used + 1 | import os as _ + 2 | import time | ~~~~ - 2 | fn main() { - 3 | println('hello, world') + 3 | + 4 | fn main() { diff --git a/vlib/v/checker/tests/import_unused_warning.vv b/vlib/v/checker/tests/import_unused_warning.vv index 1fb573fcd..f0e062d7e 100644 --- a/vlib/v/checker/tests/import_unused_warning.vv +++ b/vlib/v/checker/tests/import_unused_warning.vv @@ -1,4 +1,6 @@ +import os as _ import time + fn main() { println('hello, world') } diff --git a/vlib/v/checker/tests/incorrect_name_module.out b/vlib/v/checker/tests/incorrect_name_module.out index 4edc08ebd..5e2785851 100644 --- a/vlib/v/checker/tests/incorrect_name_module.out +++ b/vlib/v/checker/tests/incorrect_name_module.out @@ -1,3 +1,10 @@ +vlib/v/checker/tests/incorrect_name_module.vv:4:8: warning: module 'os' is imported but never used + 2 | + 3 | import math as _ + 4 | import os + | ~~ vlib/v/checker/tests/incorrect_name_module.vv:1:1: error: module name `_A` cannot start with `_` 1 | module _A - | ~~~~~~~~~ \ No newline at end of file + | ~~~~~~~~~ + 2 | + 3 | import math as _ diff --git a/vlib/v/checker/tests/incorrect_name_module.vv b/vlib/v/checker/tests/incorrect_name_module.vv index 50526ca64..a7e63ce13 100644 --- a/vlib/v/checker/tests/incorrect_name_module.vv +++ b/vlib/v/checker/tests/incorrect_name_module.vv @@ -1 +1,4 @@ module _A + +import math as _ +import os diff --git a/vlib/v/checker/tests/modules/module_alias_started_with_underscore.out b/vlib/v/checker/tests/modules/module_alias_started_with_underscore.out index a0cc315c1..5d27116aa 100644 --- a/vlib/v/checker/tests/modules/module_alias_started_with_underscore.out +++ b/vlib/v/checker/tests/modules/module_alias_started_with_underscore.out @@ -1,7 +1,7 @@ -vlib/v/checker/tests/modules/module_alias_started_with_underscore/main.v:3:1: error: module alias `_` cannot start with `_` +vlib/v/checker/tests/modules/module_alias_started_with_underscore/main.v:3:1: error: module alias `_abc` cannot start with `_` 1 | module main 2 | - 3 | import underscore as _ - | ~~~~~~~~~~~~~~~~~~~~~~ + 3 | import underscore as _abc + | ~~~~~~~~~~~~~~~~~~~~~~~~~ 4 | 5 | fn main() { diff --git a/vlib/v/checker/tests/modules/module_alias_started_with_underscore/main.v b/vlib/v/checker/tests/modules/module_alias_started_with_underscore/main.v index 39f80a1cf..2e23b8f53 100644 --- a/vlib/v/checker/tests/modules/module_alias_started_with_underscore/main.v +++ b/vlib/v/checker/tests/modules/module_alias_started_with_underscore/main.v @@ -1,7 +1,7 @@ module main -import underscore as _ +import underscore as _abc fn main() { - _.foo() + _abc.foo() } diff --git a/vlib/v/gen/c/testdata/unused_import_aliased_as_underscore.c.must_have b/vlib/v/gen/c/testdata/unused_import_aliased_as_underscore.c.must_have new file mode 100644 index 000000000..71ced9b72 --- /dev/null +++ b/vlib/v/gen/c/testdata/unused_import_aliased_as_underscore.c.must_have @@ -0,0 +1,3 @@ +struct flag__Flag { +println(flag__FlagParser_usage(fs)); +void flag__FlagParser_footer(flag__FlagParser* fs, string footer) { diff --git a/vlib/v/gen/c/testdata/unused_import_aliased_as_underscore.vv b/vlib/v/gen/c/testdata/unused_import_aliased_as_underscore.vv new file mode 100644 index 000000000..dc1265b44 --- /dev/null +++ b/vlib/v/gen/c/testdata/unused_import_aliased_as_underscore.vv @@ -0,0 +1 @@ +import flag as _ diff --git a/vlib/v/parser/module.v b/vlib/v/parser/module.v index f1016c3d6..1911d8302 100644 --- a/vlib/v/parser/module.v +++ b/vlib/v/parser/module.v @@ -58,7 +58,7 @@ fn (mut p Parser) check_unused_imports() { for import_m in p.ast_imports { alias := import_m.alias mod := import_m.mod - if !p.is_used_import(alias) { + if !(alias.len == 1 && alias[0] == `_`) && !p.is_used_import(alias) { 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) } -- 2.39.5