Skip to content

Commit

Permalink
all: do not warn/error for import flag as _
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Sep 16, 2023
1 parent 248aec3 commit 9f5f82b
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ fn (mut c Checker) check_valid_snake_case(name string, identifier string, pos to
if c.pref.translated || c.file.is_translated {
return
}
if !c.pref.is_vweb && name.len > 0 && (name[0] == `_` || name.contains('._')) {
if !c.pref.is_vweb && name.len > 1 && (name[0] == `_` || name.contains('._')) {
c.error('${identifier} `${name}` cannot start with `_`', pos)
}
if !c.pref.experimental && util.contains_capital(name) {
Expand Down
9 changes: 5 additions & 4 deletions vlib/v/checker/tests/import_unused_warning.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
vlib/v/checker/tests/import_unused_warning.vv:1:8: warning: module 'time' is imported but never used
1 | import time
vlib/v/checker/tests/import_unused_warning.vv:2:8: warning: module 'time' is imported but never used
1 | import os as _
2 | import time
| ~~~~
2 | fn main() {
3 | println('hello, world')
3 |
4 | fn main() {
2 changes: 2 additions & 0 deletions vlib/v/checker/tests/import_unused_warning.vv
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os as _
import time

fn main() {
println('hello, world')
}
9 changes: 8 additions & 1 deletion vlib/v/checker/tests/incorrect_name_module.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
vlib/v/checker/tests/incorrect_name_module.vv:4:8: warning: module 'os' is imported but never used
2 |
3 | import math as _
4 | import os
| ~~
vlib/v/checker/tests/incorrect_name_module.vv:1:1: error: module name `_A` cannot start with `_`
1 | module _A
| ~~~~~~~~~
| ~~~~~~~~~
2 |
3 | import math as _
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/incorrect_name_module.vv
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
module _A

import math as _
import os
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
vlib/v/checker/tests/modules/module_alias_started_with_underscore/main.v:3:1: error: module alias `_` cannot start with `_`
vlib/v/checker/tests/modules/module_alias_started_with_underscore/main.v:3:1: error: module alias `_abc` cannot start with `_`
1 | module main
2 |
3 | import underscore as _
| ~~~~~~~~~~~~~~~~~~~~~~
3 | import underscore as _abc
| ~~~~~~~~~~~~~~~~~~~~~~~~~
4 |
5 | fn main() {
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module main

import underscore as _
import underscore as _abc

fn main() {
_.foo()
_abc.foo()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct flag__Flag {
println(flag__FlagParser_usage(fs));
void flag__FlagParser_footer(flag__FlagParser* fs, string footer) {
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import flag as _
2 changes: 1 addition & 1 deletion vlib/v/parser/module.v
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn (mut p Parser) check_unused_imports() {
for import_m in p.ast_imports {
alias := import_m.alias
mod := import_m.mod
if !p.is_used_import(alias) {
if !(alias.len == 1 && alias[0] == `_`) && !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.mod_pos)
}
Expand Down

0 comments on commit 9f5f82b

Please sign in to comment.