Skip to content

Commit c7a6d28

Browse files
committed
all: improve unused variable warning (fix x = 1, x += 1 etc)
1 parent aa40dfc commit c7a6d28

File tree

64 files changed

+199
-141
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+199
-141
lines changed

thirdparty/sokol/sokol_app2.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

22
@implementation MyView2
33

4-
int xx = 0;
4+
int __v_sokol_inited = 0;
55

66
// Alternative drawRect which calls a frame function with native Cocoa calls
77
- (void)drawRect:(NSRect)rect {
8-
puts("drawRect()");
9-
if (xx == 0) {
8+
//puts("drawRect()");
9+
if (__v_sokol_inited == 0) {
1010
_sapp_call_init();
11-
xx = 1;
11+
__v_sokol_inited = 1;
1212
}
1313
_sapp_call_frame_native();
1414
}

vlib/builtin/builtin.c.v

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,11 @@ pub fn eprintln(s string) {
120120
C.fprintf(C.stderr, c'%.*s\n', s.len, s.str)
121121
}
122122
} $else {
123-
mut n := 0
124123
if s.str == 0 {
125-
n = C.write(2, c'eprintln(NIL)\n', 14)
124+
_ = C.write(2, c'eprintln(NIL)\n', 14)
126125
} else {
127-
n = C.write(2, s.str, s.len)
128-
n = C.write(2, c'\n', 1)
126+
_ = C.write(2, s.str, s.len)
127+
_ = C.write(2, c'\n', 1)
129128
}
130129
}
131130
C.fflush(C.stderr)
@@ -158,11 +157,10 @@ pub fn eprint(s string) {
158157
C.fprintf(C.stderr, c'%.*s', s.len, s.str)
159158
}
160159
} $else {
161-
mut n := 0
162160
if s.str == 0 {
163-
n = C.write(2, c'eprint(NIL)', 11)
161+
_ = C.write(2, c'eprint(NIL)', 11)
164162
} else {
165-
n = C.write(2, s.str, s.len)
163+
_ = C.write(2, s.str, s.len)
166164
}
167165
}
168166
C.fflush(C.stderr)
@@ -172,7 +170,6 @@ pub fn eprint(s string) {
172170
// print prints a message to stdout. Unlike `println` stdout is not automatically flushed.
173171
// A call to `flush()` will flush the output buffer to stdout.
174172
pub fn print(s string) {
175-
mut n := 0
176173
$if android {
177174
C.fprintf(C.stdout, c'%.*s', s.len, s.str)
178175
} $else $if ios {
@@ -181,7 +178,7 @@ pub fn print(s string) {
181178
} $else $if freestanding {
182179
bare_print(s.str, u64(s.len))
183180
} $else {
184-
n = C.write(1, s.str, s.len)
181+
_ = C.write(1, s.str, s.len)
185182
}
186183
}
187184

@@ -194,7 +191,6 @@ fn C.asl_log(voidptr, voidptr, int, charptr)
194191
*/
195192
// println prints a message with a line end, to stdout. stdout is flushed.
196193
pub fn println(s string) {
197-
mut n := 0
198194
if s.str == 0 {
199195
$if android {
200196
C.fprintf(C.stdout, c'println(NIL)\n')
@@ -204,7 +200,7 @@ pub fn println(s string) {
204200
bare_print(s.str, u64(s.len))
205201
bare_print(c'println(NIL)\n', 13)
206202
} $else {
207-
n = C.write(1, c'println(NIL)\n', 13)
203+
_ = C.write(1, c'println(NIL)\n', 13)
208204
}
209205
return
210206
}
@@ -216,8 +212,8 @@ pub fn println(s string) {
216212
bare_print(s.str, u64(s.len))
217213
bare_print(c'\n', 1)
218214
} $else {
219-
n = C.write(1, s.str, s.len)
220-
n = C.write(1, c'\n', 1)
215+
_ = C.write(1, s.str, s.len)
216+
_ = C.write(1, c'\n', 1)
221217
}
222218
}
223219

vlib/builtin/builtin_nix.c.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,6 @@ fn break_if_debugger_attached() {
145145
unsafe {
146146
mut ptr := &voidptr(0)
147147
*ptr = voidptr(0)
148+
_ = ptr
148149
}
149150
}

vlib/os/os_c.v

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,11 +740,10 @@ pub fn is_link(path string) bool {
740740

741741
// chdir changes the current working directory to the new directory in `path`.
742742
pub fn chdir(path string) {
743-
mut n := 0
744743
$if windows {
745744
C._wchdir(path.to_wide())
746745
} $else {
747-
n = C.chdir(&char(path.str))
746+
_ = C.chdir(&char(path.str))
748747
}
749748
}
750749

vlib/os/process_nix.c.v

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ module os
33
fn C.setpgid(pid int, pgid int) int
44

55
fn (mut p Process) unix_spawn_process() int {
6-
mut n := 0
76
mut pipeset := [6]int{}
87
if p.use_stdio_ctl {
9-
n = C.pipe(&pipeset[0]) // pipe read end 0 <- 1 pipe write end
10-
n = C.pipe(&pipeset[2]) // pipe read end 2 <- 3 pipe write end
11-
n = C.pipe(&pipeset[4]) // pipe read end 4 <- 5 pipe write end
8+
_ = C.pipe(&pipeset[0]) // pipe read end 0 <- 1 pipe write end
9+
_ = C.pipe(&pipeset[2]) // pipe read end 2 <- 3 pipe write end
10+
_ = C.pipe(&pipeset[4]) // pipe read end 4 <- 5 pipe write end
1211
}
1312
pid := fork()
1413
if pid != 0 {

vlib/v/cflag/cflags.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ pub fn (cf &CFlag) eval() string {
3333
sparams := remainder[cflag.fexisting_literal.len + 1..].all_before(')')
3434
i += sparams.len + cflag.fexisting_literal.len + 1
3535
svalues := sparams.replace(',', '\n').split_into_lines().map(it.trim(' \'"'))
36-
mut found_spath := ''
36+
// mut found_spath := ''
3737
for spath in svalues {
3838
if os.exists(spath) {
39-
found_spath = spath
39+
// found_spath = spath
4040
value += spath
4141
continue cflag_eval_outer_loop
4242
}

vlib/v/checker/checker.v

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub fn (mut c Checker) check_scope_vars(sc &ast.Scope) {
119119
match obj {
120120
ast.Var {
121121
if !c.pref.is_repl {
122-
if !obj.is_used && obj.name[0] != `_` {
122+
if !obj.is_used && obj.name[0] != `_` && !c.file.is_test {
123123
c.warn('unused variable: `$obj.name`', obj.pos)
124124
}
125125
}
@@ -2570,7 +2570,10 @@ pub fn (mut c Checker) selector_expr(mut selector_expr ast.SelectorExpr) ast.Typ
25702570
//
25712571
c.using_new_err_struct = using_new_err_struct_save
25722572
if typ == ast.void_type_idx {
2573-
c.error('unknown selector expression', selector_expr.pos)
2573+
// This means that the variable's value was assigned to an
2574+
// unknown function or method, so the error was already handled
2575+
// earlier
2576+
// c.error('unknown selector expression', selector_expr.pos)
25742577
return ast.void_type
25752578
}
25762579
selector_expr.expr_type = typ
@@ -3644,7 +3647,7 @@ fn (mut c Checker) stmt(node ast.Stmt) {
36443647
fn (mut c Checker) assert_stmt(node ast.AssertStmt) {
36453648
cur_exp_typ := c.expected_type
36463649
assert_type := c.check_expr_opt_call(node.expr, c.expr(node.expr))
3647-
if assert_type != ast.bool_type_idx {
3650+
if assert_type != ast.bool_type_idx && assert_type != ast.void_type_idx {
36483651
atype_name := c.table.get_type_symbol(assert_type).name
36493652
c.error('assert can be used only with `bool` expressions, but found `$atype_name` instead',
36503653
node.pos)
@@ -4763,7 +4766,7 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) ast.Type {
47634766
}
47644767
ast.Var {
47654768
// incase var was not marked as used yet (vweb tmpl)
4766-
obj.is_used = true
4769+
// obj.is_used = true
47674770
if ident.pos.pos < obj.pos.pos {
47684771
c.error('undefined variable `$ident.name` (used before declaration)',
47694772
ident.pos)

vlib/v/checker/tests/array_or_map_assign_err.out

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ vlib/v/checker/tests/array_or_map_assign_err.vv:5:5: error: use `array2 = array1
1010
4 | mut a3 := []int{}
1111
5 | a3 = a1
1212
| ^
13-
6 |
13+
6 |
1414
7 | m1 := {'one': 1}
1515
vlib/v/checker/tests/array_or_map_assign_err.vv:8:8: error: cannot copy map: call `move` or `clone` method (or use a reference)
16-
6 |
16+
6 |
1717
7 | m1 := {'one': 1}
1818
8 | m2 := m1
1919
| ~~
@@ -24,12 +24,12 @@ vlib/v/checker/tests/array_or_map_assign_err.vv:10:7: error: cannot copy map: ca
2424
9 | mut m3 := map[string]int{}
2525
10 | m3 = m1
2626
| ~~
27-
11 |
27+
11 |
2828
12 | _ = a2
29-
vlib/v/checker/tests/array_or_map_assign_err.vv:20:8: error: cannot copy map: call `move` or `clone` method (or use a reference)
30-
18 |
31-
19 | fn foo(mut m map[string]int) {
32-
20 | m2 := m
29+
vlib/v/checker/tests/array_or_map_assign_err.vv:25:8: error: cannot copy map: call `move` or `clone` method (or use a reference)
30+
23 |
31+
24 | fn foo(mut m map[string]int) {
32+
25 | m2 := m
3333
| ^
34-
21 | m['foo'] = 100
35-
22 | println(m)
34+
26 | m['foo'] = 100
35+
27 | println(m)

vlib/v/checker/tests/array_or_map_assign_err.vv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ fn main() {
1414

1515
mut m := {'foo':1}
1616
foo(mut m)
17+
18+
_ = a3
19+
_ = m1
20+
_ = m2
21+
_ = m3
1722
}
1823

1924
fn foo(mut m map[string]int) {

vlib/v/checker/tests/arrow_op_wrong_left_type_err_b.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ vlib/v/checker/tests/arrow_op_wrong_left_type_err_b.vv:4:8: error: cannot assign
33
3 | mut obj := 9
44
4 | obj = <-ch
55
| ~~
6-
5 | }
6+
5 | _ = obj
7+
6 | }

0 commit comments

Comments
 (0)