parser: warning about unused imports

pull/4895/head
yuyi 2020-05-14 23:14:24 +08:00 committed by GitHub
parent c3fe2135a4
commit 6d0b791ac8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 46 additions and 27 deletions

View File

@ -1,7 +1,6 @@
module main module main
import os import os
import term
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
// / This file will get compiled as part of the main program, // / This file will get compiled as part of the main program,
// / for a _test.v file. // / for a _test.v file.

View File

@ -4,7 +4,6 @@
module ast module ast
import v.table import v.table
import v.token
pub struct Scope { pub struct Scope {
//mut: //mut:

View File

@ -1,7 +1,6 @@
module builder module builder
import os import os
import time
import v.ast import v.ast
import v.table import v.table
import v.pref import v.pref
@ -9,10 +8,6 @@ import v.util
import v.vmod import v.vmod
import v.checker import v.checker
import v.parser import v.parser
import v.errors
import v.gen
import v.gen.js
import v.gen.x64
import v.depgraph import v.depgraph
const ( const (

View File

@ -6,8 +6,6 @@ module builder
import time import time
import os import os
import v.pref import v.pref
import v.util
import strings
fn get_vtmp_folder() string { fn get_vtmp_folder() string {
vtmp := os.join_path(os.temp_dir(), 'v') vtmp := os.join_path(os.temp_dir(), 'v')

View File

@ -1,7 +1,6 @@
module builder module builder
import time import time
import os
import v.parser import v.parser
import v.pref import v.pref
import v.gen import v.gen

View File

@ -4,13 +4,11 @@
module checker module checker
import v.ast import v.ast
import v.depgraph
import v.table import v.table
import v.token import v.token
import v.pref import v.pref
import v.util import v.util
import v.errors import v.errors
import os
const ( const (
max_nr_errors = 300 max_nr_errors = 300

View File

@ -1,4 +1,4 @@
import notexist import notexist
fn main() { fn main() {
println('hello, world') println(notexist.name)
} }

View File

@ -0,0 +1,2 @@
`vlib/v/checker/tests/import_unused_warning.v` warning: the following imports were never used:
* time

View File

@ -0,0 +1,4 @@
import time
fn main() {
println('hello, world')
}

View File

@ -1,7 +1,5 @@
module gen module gen
import v.util
// NB: @@@ here serve as placeholders. // NB: @@@ here serve as placeholders.
// They will be replaced with correct strings // They will be replaced with correct strings
// for each constant, during C code generation. // for each constant, during C code generation.

View File

@ -3,8 +3,6 @@ module js
import strings import strings
import v.ast import v.ast
import v.table import v.table
import v.depgraph
import v.token
import v.pref import v.pref
import term import term
import v.util import v.util

View File

@ -1,7 +1,5 @@
module gen module gen
import os
import time
import v.pref import v.pref
import v.util import v.util

View File

@ -4,8 +4,6 @@
module parser module parser
import v.ast import v.ast
import v.table
import v.token
fn (mut p Parser) assign_stmt() ast.Stmt { fn (mut p Parser) assign_stmt() ast.Stmt {
is_static := p.tok.kind == .key_static is_static := p.tok.kind == .key_static

View File

@ -5,7 +5,6 @@ module parser
import v.ast import v.ast
import v.table import v.table
import v.token
fn (mut p Parser) for_stmt() ast.Stmt { fn (mut p Parser) for_stmt() ast.Stmt {
p.check(.key_for) p.check(.key_for)

View File

@ -17,3 +17,27 @@ fn (p &Parser) prepend_mod(name string) string {
} }
return '${p.mod}.$name' return '${p.mod}.$name'
} }
fn (p &Parser) is_used_import(alias string) bool {
return alias in p.used_imports
}
fn (mut p Parser) register_used_import(alias string) {
if alias !in p.used_imports {
p.used_imports << alias
}
}
fn (p mut 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
}
eprintln('`$p.file_name` warning: the following imports were never used: $output')
}

View File

@ -23,7 +23,7 @@ pub fn (mut p Parser) parse_array_type() table.Type {
// detect attr // detect attr
not_attr := p.peek_tok.kind != .name && p.peek_tok2.kind !in [.semicolon, .rsbr] not_attr := p.peek_tok.kind != .name && p.peek_tok2.kind !in [.semicolon, .rsbr]
for p.tok.kind == .lsbr && not_attr { for p.tok.kind == .lsbr && not_attr {
p.next() p.next()
p.check(.rsbr) p.check(.rsbr)
@ -146,6 +146,9 @@ pub fn (mut p Parser) parse_any_type(is_c, is_js, is_ptr bool) table.Type {
println(p.table.imports) println(p.table.imports)
p.error('unknown module `$p.tok.lit`') p.error('unknown module `$p.tok.lit`')
} }
if p.tok.lit in p.imports {
p.register_used_import(p.tok.lit)
}
p.next() p.next()
p.check(.dot) p.check(.dot)
// prefix with full module // prefix with full module

View File

@ -10,7 +10,6 @@ import v.table
import v.pref import v.pref
import v.util import v.util
import v.errors import v.errors
import term
import os import os
pub struct Parser { pub struct Parser {
@ -38,8 +37,9 @@ mut:
expr_mod string // for constructing full type names in parse_type() expr_mod string // for constructing full type names in parse_type()
scope &ast.Scope scope &ast.Scope
global_scope &ast.Scope global_scope &ast.Scope
imports map[string]string imports map[string]string // alias => mod_name
ast_imports []ast.Import ast_imports []ast.Import // mod_names
used_imports []string // alias
is_amp bool // for generating the right code for `&Foo{}` is_amp bool // for generating the right code for `&Foo{}`
returns bool returns bool
inside_match bool // to separate `match A { }` from `Struct{}` inside_match bool // to separate `match A { }` from `Struct{}`
@ -115,6 +115,8 @@ pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.Comme
file: p.file_name file: p.file_name
return_type: table.void_type return_type: table.void_type
} }
} else {
p.check_unused_imports()
} }
break break
} }
@ -285,6 +287,9 @@ fn (mut p Parser) check(expected token.Kind) {
fn (mut p Parser) check_name() string { fn (mut p Parser) check_name() string {
name := p.tok.lit name := p.tok.lit
if p.peek_tok.kind == .dot && name in p.imports {
p.register_used_import(name)
}
p.check(.name) p.check(.name)
return name return name
} }
@ -697,6 +702,9 @@ pub fn (mut p Parser) name_expr() ast.Expr {
} else if is_js { } else if is_js {
mod = 'JS' mod = 'JS'
} else { } else {
if p.tok.lit in p.imports {
p.register_used_import(p.tok.lit)
}
// prepend the full import // prepend the full import
mod = p.imports[p.tok.lit] mod = p.imports[p.tok.lit]
} }

View File

@ -4,7 +4,6 @@
module pref module pref
import os import os
import term
pub const ( pub const (
default_module_path = mpath() default_module_path = mpath()