mirror of
https://github.com/vlang/v.git
synced 2025-09-14 23:12:33 +03:00
compiler: error on unused imports
This commit is contained in:
parent
71484e89d6
commit
23c84516e2
29 changed files with 112 additions and 55 deletions
|
@ -6,7 +6,6 @@ module main
|
|||
|
||||
import (
|
||||
os
|
||||
rand
|
||||
strings
|
||||
)
|
||||
|
||||
|
@ -29,7 +28,7 @@ mut:
|
|||
lit string
|
||||
cgen &CGen
|
||||
table &Table
|
||||
import_table &FileImportTable // Holds imports for just the file being parsed
|
||||
import_table FileImportTable // Holds imports for just the file being parsed
|
||||
pass Pass
|
||||
os OS
|
||||
mod string
|
||||
|
@ -92,7 +91,7 @@ fn (v mut V) new_parser(path string) Parser {
|
|||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
mut p := Parser {
|
||||
v: v
|
||||
file_path: path
|
||||
|
@ -101,7 +100,7 @@ fn (v mut V) new_parser(path string) Parser {
|
|||
file_pcguard: path_pcguard
|
||||
scanner: new_scanner(path)
|
||||
table: v.table
|
||||
import_table: new_file_import_table(path)
|
||||
import_table: v.table.get_file_import_table(path)
|
||||
cur_fn: EmptyFn
|
||||
cgen: v.cgen
|
||||
is_script: (v.pref.is_script && path == v.dir)
|
||||
|
@ -186,7 +185,7 @@ fn (p mut Parser) parse(pass Pass) {
|
|||
p.error('module `builtin` cannot be imported')
|
||||
}
|
||||
// save file import table
|
||||
p.table.file_imports << *p.import_table
|
||||
p.table.file_imports[p.file_path] = p.import_table
|
||||
return
|
||||
}
|
||||
// Go through every top level token or throw a compilation error if a non-top level token is met
|
||||
|
@ -277,6 +276,9 @@ fn (p mut Parser) parse(pass Pass) {
|
|||
if p.is_script && !p.pref.is_test {
|
||||
p.set_current_fn( MainFn )
|
||||
p.check_unused_variables()
|
||||
if !p.first_pass() && !p.pref.is_repl {
|
||||
p.check_unused_imports()
|
||||
}
|
||||
}
|
||||
if false && !p.first_pass() && p.fileis('main.v') {
|
||||
out := os.create('/var/tmp/fmt.v') or {
|
||||
|
@ -635,6 +637,7 @@ fn (p mut Parser) struct_decl() {
|
|||
access_mod := if is_pub{AccessMod.public} else { AccessMod.private}
|
||||
p.fgen(' ')
|
||||
field_type := p.get_type()
|
||||
p.check_and_register_used_imported_type(field_type)
|
||||
is_atomic := p.tok == .key_atomic
|
||||
if is_atomic {
|
||||
p.next()
|
||||
|
@ -1555,6 +1558,7 @@ fn (p mut Parser) name_expr() string {
|
|||
mut mod := name
|
||||
// must be aliased module
|
||||
if name != p.mod && p.import_table.known_alias(name) {
|
||||
p.import_table.register_used_import(name)
|
||||
// we replaced "." with "_dot_" in p.mod for C variable names, do same here.
|
||||
mod = p.import_table.resolve_alias(name).replace('.', '_dot_')
|
||||
}
|
||||
|
@ -2388,6 +2392,7 @@ fn (p mut Parser) factor() string {
|
|||
if !('json' in p.table.imports) {
|
||||
p.error('undefined: `json`, use `import json`')
|
||||
}
|
||||
p.import_table.register_used_import('json')
|
||||
return p.js_decode()
|
||||
}
|
||||
//if p.fileis('orm_test') {
|
||||
|
@ -3801,3 +3806,29 @@ fn (p mut Parser) defer_st() {
|
|||
p.cgen.resetln('')
|
||||
}
|
||||
|
||||
fn (p mut Parser) check_and_register_used_imported_type(typ_name string) {
|
||||
us_idx := typ_name.index('__')
|
||||
if us_idx != -1 {
|
||||
arg_mod := typ_name.left(us_idx)
|
||||
if p.import_table.known_alias(arg_mod) {
|
||||
p.import_table.register_used_import(arg_mod)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn (p mut Parser) check_unused_imports() {
|
||||
mut output := ''
|
||||
for alias, mod in p.import_table.imports {
|
||||
if !p.import_table.is_used_import(alias) {
|
||||
mod_alias := if alias == mod { alias } else { '$alias ($mod)' }
|
||||
output += '\n * $mod_alias'
|
||||
}
|
||||
}
|
||||
if output == '' { return }
|
||||
output = '$p.file_path: the following imports were never used:$output'
|
||||
if p.pref.is_prod {
|
||||
cerror(output)
|
||||
} else {
|
||||
println('warning: $output')
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue