parser: add an error for `import mod.sub as mod` and `import mod as mod` (#6194)

pull/6197/head
Swastik Baranwal 2020-08-23 05:42:25 +05:30 committed by GitHub
parent 61df70fdf5
commit b2e79dbebd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 26 additions and 3 deletions

View File

@ -1,5 +1,5 @@
// Simple raw HTTP head request // Simple raw HTTP head request
import x.net as net import x.net
import time import time
// Make a new connection // Make a new connection

View File

@ -1,4 +1,4 @@
import math.stats as stats import math.stats
import math import math
fn test_freq() { fn test_freq() {

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/import_mod_as_mod_err.v:1:16: error: import alias `math as math` is redundant
1 | import math as math
| ~~~~
2 |
3 | fn main() {

View File

@ -0,0 +1,5 @@
import math as math
fn main() {
println(math.e)
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/import_mod_sub_as_sub_err.v:1:25: error: import alias `encoding.utf8 as utf8` is redundant
1 | import encoding.utf8 as utf8
| ~~~~
2 |
3 | fn main() {

View File

@ -0,0 +1,5 @@
import encoding.utf8 as utf8
fn main() {
println(utf8.validate_str('añçá'))
}

View File

@ -1495,6 +1495,9 @@ fn (mut p Parser) import_stmt() ast.Import {
if p.tok.kind == .key_as { if p.tok.kind == .key_as {
p.next() p.next()
mod_alias = p.check_name() mod_alias = p.check_name()
if mod_alias == mod_name.split('.').last() {
p.error_with_pos('import alias `$mod_name as $mod_alias` is redundant', p.prev_tok.position())
}
} }
node := ast.Import{ node := ast.Import{
pos: pos pos: pos

View File

@ -1,4 +1,4 @@
import x.net as net import x.net
import time import time
fn echo_server(_c net.UdpConn) { fn echo_server(_c net.UdpConn) {