parser: improve unused imports warning

pull/4967/head
Don Alfons Nisnoni 2020-05-21 01:50:27 +08:00 committed by GitHub
parent 9d4fe88d09
commit 10ad5332e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 14 deletions

View File

@ -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')

View File

@ -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)
}
}
}