From 10ad5332e883869aa035bca041aa90f0a130450b Mon Sep 17 00:00:00 2001 From: Don Alfons Nisnoni Date: Thu, 21 May 2020 01:50:27 +0800 Subject: [PATCH] parser: improve unused imports warning --- .../v/checker/tests/import_unused_warning.out | 7 +++++-- vlib/v/parser/module.v | 21 ++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/vlib/v/checker/tests/import_unused_warning.out b/vlib/v/checker/tests/import_unused_warning.out index 58ead92609..4ab7bbbf5a 100644 --- a/vlib/v/checker/tests/import_unused_warning.out +++ b/vlib/v/checker/tests/import_unused_warning.out @@ -1,2 +1,5 @@ -`vlib/v/checker/tests/import_unused_warning.v` warning: the following imports were never used: - * time +vlib/v/checker/tests/import_unused_warning.v:1:8: warning: module 'time' is imported but never used + 1 | import time + | ~~~~ + 2 | fn main() { + 3 | println('hello, world') diff --git a/vlib/v/parser/module.v b/vlib/v/parser/module.v index b5b23ce42c..f9736ffe02 100644 --- a/vlib/v/parser/module.v +++ b/vlib/v/parser/module.v @@ -23,26 +23,23 @@ fn (p &Parser) is_used_import(alias string) bool { } fn (mut p Parser) register_used_import(alias string) { - if alias !in p.used_imports { + if !p.is_used_import(alias) { p.used_imports << alias } } fn (mut p Parser) check_unused_imports() { - mut output := '' - for alias, mod in p.imports { - if !p.is_used_import(alias) { - mod_alias := if alias == mod { alias } else { '$alias ($mod)' } - output += '\n * $mod_alias' - } - } - if output == '' { - return - } if p.pref.is_repl { // The REPL should be much more liberal, and should not warn about // unused imports, because they probably will be in the next few lines... return } - eprintln('`$p.file_name` warning: the following imports were never used: $output') + for import_m in p.ast_imports { + alias := import_m.alias + mod := import_m.mod + if !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.pos) + } + } }