1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: warning about unused imports

This commit is contained in:
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
import os
import term
// //////////////////////////////////////////////////////////////////
// / This file will get compiled as part of the main program,
// / for a _test.v file.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
import notexist
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
import v.util
// NB: @@@ here serve as placeholders.
// They will be replaced with correct strings
// for each constant, during C code generation.

View File

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

View File

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

View File

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

View File

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

View File

@ -17,3 +17,27 @@ fn (p &Parser) prepend_mod(name string) string {
}
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
not_attr := p.peek_tok.kind != .name && p.peek_tok2.kind !in [.semicolon, .rsbr]
for p.tok.kind == .lsbr && not_attr {
p.next()
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)
p.error('unknown module `$p.tok.lit`')
}
if p.tok.lit in p.imports {
p.register_used_import(p.tok.lit)
}
p.next()
p.check(.dot)
// prefix with full module

View File

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

View File

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