checker: check return_duplicate_with_none (closes #5363)

pull/5383/head
yuyi 2020-06-15 13:10:45 +08:00 committed by GitHub
parent b93177c1a8
commit 2785a5bf65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 3 deletions

View File

@ -798,7 +798,8 @@ pub mut:
pub struct None {
pub:
foo int // todo
pos token.Position
foo int // todo
}
[inline]
@ -874,6 +875,9 @@ pub fn (expr Expr) position() token.Position {
MatchExpr {
return it.pos
}
None {
return it.pos
}
PostfixExpr {
return it.pos
}

View File

@ -1793,7 +1793,7 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) {
c.stmt(stmt)
}
if unreachable.line_nr >= 0 {
c.warn('unreachable code', unreachable)
c.error('unreachable code', unreachable)
}
c.scope_returns = false
c.expected_type = table.void_type

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/return_duplicate_with_none_err_a.v:3:2: error: unreachable code
1 | fn f() ?bool {
2 | return true
3 | return none
| ~~~~~~~~~~~
4 | }
5 | fn main() {

View File

@ -0,0 +1,9 @@
fn f() ?bool {
return true
return none
}
fn main() {
f() or {
println('fail')
}
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/return_duplicate_with_none_err_b.v:3:2: error: unreachable code
1 | fn f() ?bool {
2 | return none
3 | return true
| ~~~~~~~~~~~
4 | }
5 | fn main() {

View File

@ -0,0 +1,9 @@
fn f() ?bool {
return none
return true
}
fn main() {
f() or {
println('fail')
}
}

View File

@ -73,8 +73,11 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
}
}
.key_none {
pos := p.tok.position()
p.next()
node = ast.None{}
node = ast.None{
pos: pos
}
}
.key_sizeof {
p.next() // sizeof