mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: improve unused imports warning
This commit is contained in:
parent
9d4fe88d09
commit
10ad5332e8
@ -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')
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user