mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: disallow using unsafe { none }
(#16431)
This commit is contained in:
parent
fe2db64384
commit
dc81d755e8
@ -54,7 +54,13 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
|
||||
}
|
||||
}
|
||||
if mut right is ast.None {
|
||||
c.error('you can not assign a `none` value to a variable', right.pos)
|
||||
c.error('cannot assign a `none` value to a variable', right.pos)
|
||||
}
|
||||
// Handle `left_name := unsafe { none }`
|
||||
if mut right is ast.UnsafeExpr {
|
||||
if mut right.expr is ast.None {
|
||||
c.error('cannot use `none` in `unsafe` blocks', right.expr.pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
if node.left.len != right_len {
|
||||
|
@ -42,6 +42,12 @@ pub fn (mut c Checker) return_stmt(mut node ast.Return) {
|
||||
if typ == 0 {
|
||||
return
|
||||
}
|
||||
// Handle `return unsafe { none }`
|
||||
if expr is ast.UnsafeExpr {
|
||||
if expr.expr is ast.None {
|
||||
c.error('cannot return `none` in unsafe block', expr.expr.pos)
|
||||
}
|
||||
}
|
||||
if typ == ast.void_type {
|
||||
c.error('`${expr}` used as value', node.pos)
|
||||
return
|
||||
|
@ -1,4 +1,4 @@
|
||||
vlib/v/checker/tests/assing_none.vv:2:9: error: you can not assign a `none` value to a variable
|
||||
vlib/v/checker/tests/assing_none.vv:2:9: error: cannot assign a `none` value to a variable
|
||||
1 | fn main() {
|
||||
2 | val := none
|
||||
| ~~~~
|
||||
|
6
vlib/v/checker/tests/unsafe_assign_none_err.out
Normal file
6
vlib/v/checker/tests/unsafe_assign_none_err.out
Normal file
@ -0,0 +1,6 @@
|
||||
vlib/v/checker/tests/unsafe_assign_none_err.vv:2:20: error: cannot use `none` in `unsafe` blocks
|
||||
1 | fn f() {
|
||||
2 | mut s := unsafe { none }
|
||||
| ~~~~
|
||||
3 | println(s)
|
||||
4 | }
|
6
vlib/v/checker/tests/unsafe_assign_none_err.vv
Normal file
6
vlib/v/checker/tests/unsafe_assign_none_err.vv
Normal file
@ -0,0 +1,6 @@
|
||||
fn f() {
|
||||
mut s := unsafe { none }
|
||||
println(s)
|
||||
}
|
||||
|
||||
f()
|
5
vlib/v/checker/tests/unsafe_none_return_err.out
Normal file
5
vlib/v/checker/tests/unsafe_none_return_err.out
Normal file
@ -0,0 +1,5 @@
|
||||
vlib/v/checker/tests/unsafe_none_return_err.vv:2:18: error: cannot return `none` in unsafe block
|
||||
1 | fn f() ?int {
|
||||
2 | return unsafe { none }
|
||||
| ~~~~
|
||||
3 | }
|
3
vlib/v/checker/tests/unsafe_none_return_err.vv
Normal file
3
vlib/v/checker/tests/unsafe_none_return_err.vv
Normal file
@ -0,0 +1,3 @@
|
||||
fn f() ?int {
|
||||
return unsafe { none }
|
||||
}
|
Loading…
Reference in New Issue
Block a user