v.checker: fix `unused variable` warning on `*p = val`
parent
8361f714dd
commit
cf3dd7a51f
|
@ -121,21 +121,21 @@ pub fn (mut c Checker) check(ast_file &ast.File) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut c Checker) check_scope_vars(sc &ast.Scope) {
|
pub fn (mut c Checker) check_scope_vars(sc &ast.Scope) {
|
||||||
for _, obj in sc.objects {
|
if !c.pref.is_repl && !c.file.is_test {
|
||||||
match obj {
|
for _, obj in sc.objects {
|
||||||
ast.Var {
|
match obj {
|
||||||
if !c.pref.is_repl {
|
ast.Var {
|
||||||
if !obj.is_used && obj.name[0] != `_` && !c.file.is_test {
|
if !obj.is_used && obj.name[0] != `_` {
|
||||||
c.warn('unused variable: `$obj.name`', obj.pos)
|
c.warn('unused variable: `$obj.name`', obj.pos)
|
||||||
}
|
}
|
||||||
|
if obj.is_mut && !obj.is_changed && !c.is_builtin_mod && obj.name != 'it' {
|
||||||
|
// if obj.is_mut && !obj.is_changed && !c.is_builtin { //TODO C error bad field not checked
|
||||||
|
// c.warn('`$obj.name` is declared as mutable, but it was never changed',
|
||||||
|
// obj.pos)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if obj.is_mut && !obj.is_changed && !c.is_builtin_mod && obj.name != 'it' {
|
else {}
|
||||||
// if obj.is_mut && !obj.is_changed && !c.is_builtin { //TODO C error bad field not checked
|
|
||||||
// c.warn('`$obj.name` is declared as mutable, but it was never changed',
|
|
||||||
// obj.pos)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for child in sc.children {
|
for child in sc.children {
|
||||||
|
@ -3323,9 +3323,24 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
|
||||||
}
|
}
|
||||||
ast.PrefixExpr {
|
ast.PrefixExpr {
|
||||||
// Do now allow `*x = y` outside `unsafe`
|
// Do now allow `*x = y` outside `unsafe`
|
||||||
if left.op == .mul && !c.inside_unsafe {
|
if left.op == .mul {
|
||||||
c.error('modifying variables via dereferencing can only be done in `unsafe` blocks',
|
if !c.inside_unsafe {
|
||||||
assign_stmt.pos)
|
c.error('modifying variables via dereferencing can only be done in `unsafe` blocks',
|
||||||
|
assign_stmt.pos)
|
||||||
|
} else {
|
||||||
|
// mark `p` in `*p = val` as used:
|
||||||
|
match mut left.right {
|
||||||
|
ast.Ident {
|
||||||
|
match mut left.right.obj {
|
||||||
|
ast.Var {
|
||||||
|
left.right.obj.is_used = true
|
||||||
|
}
|
||||||
|
else {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if is_decl {
|
if is_decl {
|
||||||
c.error('non-name on the left side of `:=`', left.pos)
|
c.error('non-name on the left side of `:=`', left.pos)
|
||||||
|
|
|
@ -72,3 +72,32 @@ fn test_unsafe_if_stmt() {
|
||||||
x := unsafe_if_stmt()
|
x := unsafe_if_stmt()
|
||||||
assert x == 4
|
assert x == 4
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fixedbytes = [100]byte{}
|
||||||
|
|
||||||
|
fn test_unsafe_pointers() {
|
||||||
|
fsize := fixedbytes.len
|
||||||
|
src := &fixedbytes[0]
|
||||||
|
//
|
||||||
|
b := []byte{}
|
||||||
|
eprintln('b.data before: $b.data')
|
||||||
|
eprintln('b.len before: $b.len')
|
||||||
|
eprintln('b.cap before: $b.cap')
|
||||||
|
assert b.len == 0
|
||||||
|
unsafe {
|
||||||
|
// here b will be setup to work with the mmaped region
|
||||||
|
mut pdata := &b.data
|
||||||
|
mut plen := &b.len
|
||||||
|
mut pcap := &b.cap
|
||||||
|
// note that pdata, plen, pcap are used here:
|
||||||
|
*pdata = src
|
||||||
|
*plen = int(fsize)
|
||||||
|
*pcap = int(fsize)
|
||||||
|
}
|
||||||
|
assert b.len == 100
|
||||||
|
assert b.cap == 100
|
||||||
|
assert b.data == src
|
||||||
|
eprintln('b.data after: $b.data')
|
||||||
|
eprintln('b.len after: $b.len')
|
||||||
|
eprintln('b.cap after: $b.cap')
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue