Skip to content

Commit

Permalink
parser: warn on unused imports, even when they are declared via `impo…
Browse files Browse the repository at this point in the history
…rt math { sin, cos }` (vlang#19738)
  • Loading branch information
Delta456 authored Nov 4, 2023
1 parent 59ffee3 commit 14afda7
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions vlib/v/checker/tests/import_symbol_fn_err.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
vlib/v/checker/tests/import_symbol_fn_err.vv:1:8: warning: module 'crypto' is imported but never used
1 | import crypto { userper }
| ~~~~~~
2 | fn main() {
3 | usurper()
vlib/v/checker/tests/import_symbol_fn_err.vv:1:17: error: module `crypto` has no constant or function `userper`
1 | import crypto { userper }
| ~~~~~~~
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/selective_const_import.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
vlib/v/checker/tests/selective_const_import.vv:1:8: warning: module 'time' is imported but never used
1 | import time { second }
| ~~~~
vlib/v/checker/tests/selective_const_import.vv:1:15: error: cannot selectively import constant `second` from `time`, import `time` and use `time.second` instead
1 | import time { second }
| ~~~~~~
1 change: 1 addition & 0 deletions vlib/v/parser/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ fn (mut p Parser) call_expr(language ast.Language, mod string) ast.CallExpr {
}
if fn_name in p.imported_symbols {
fn_name = p.imported_symbols[fn_name]
p.register_used_import_for_symbol_name(fn_name)
}
comments := p.eat_comments(same_line: true)
pos.update_last_line(p.prev_tok.line_nr)
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/parser/module.v
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ fn (mut p Parser) register_used_import(alias string) {
}
}

fn (mut p Parser) register_used_import_for_symbol_name(sym_name string) {
short_import_name := sym_name.all_before_last('.').all_after_last('.')
p.register_used_import(short_import_name)
}

fn (mut p Parser) register_auto_import(alias string) {
if alias !in p.imports {
p.imports[alias] = alias
Expand Down
1 change: 1 addition & 0 deletions vlib/v/parser/parse_type.v
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_dot b
name = p.expr_mod + '.' + name
} else if name in p.imported_symbols {
name = p.imported_symbols[name]
p.register_used_import_for_symbol_name(name)
} else if !p.builtin_mod && name.len > 1 && name !in p.table.type_idxs {
// `Foo` in module `mod` means `mod.Foo`
name = p.mod + '.' + name
Expand Down
1 change: 0 additions & 1 deletion vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -3699,7 +3699,6 @@ fn (mut p Parser) import_stmt() ast.Import {
syms_pos: initial_syms_pos
pos: import_node.pos.extend(initial_syms_pos)
}
p.register_used_import(mod_alias) // no `unused import` msg for parent
}
pos_t := p.tok.pos()
if import_pos.line_nr == pos_t.line_nr {
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/parser/tests/import_sym_fn_unused_warning_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vlib/v/parser/tests/import_sym_fn_unused_warning_err.vv:1:8: warning: module 'math' is imported but never used
1 | import math { sin, cos }
| ~~~~
2 |
3 | fn main() {}
3 changes: 3 additions & 0 deletions vlib/v/parser/tests/import_sym_fn_unused_warning_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import math { sin, cos }

fn main() {}

0 comments on commit 14afda7

Please sign in to comment.