From ddd4ac7880f392ad6c39719af05f01d29eb0ad3a Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 27 May 2026 05:32:18 +0300 Subject: [PATCH] all: more shadowing fixes --- vlib/arrays/arrays.v | 25 +++---- vlib/crypto/sha512/sha512.v | 6 +- vlib/datatypes/doubly_linked_list.v | 24 +++--- vlib/datatypes/linked_list.v | 26 +++---- vlib/gg/m4/matrix.v | 6 +- vlib/math/stats/stats.v | 106 +++++++++++++-------------- vlib/net/s3/client.v | 8 +- vlib/os/os.js.v | 12 +-- vlib/os/os.v | 6 +- vlib/os/os_js.js.v | 12 +-- vlib/os/os_windows.c.v | 18 ++--- vlib/sokol/sapp/sapp_egl_linux.v | 6 +- vlib/sokol/sapp/sapp_wayland_linux.v | 20 ++--- vlib/sokol/sapp/sapp_x11_linux.v | 8 +- vlib/v/checker/assign.v | 14 ++-- 15 files changed, 150 insertions(+), 147 deletions(-) diff --git a/vlib/arrays/arrays.v b/vlib/arrays/arrays.v index 48283fdfb..9f4695d0e 100644 --- a/vlib/arrays/arrays.v +++ b/vlib/arrays/arrays.v @@ -190,21 +190,20 @@ pub fn chunk_while[T](a []T, predicate fn (before T, after T) bool) [][]T { return [] } mut chunks := [][]T{} - mut chunk := [a[0]] + mut current := [a[0]] mut i := 0 for i = 1; i < a.len; i++ { - // eprintln('> i: ${i} | a[i]: ${a[i]} | predicate: ${predicate(a[i-1], a[i]):10} | chunk: ${chunk}') if predicate(a[i - 1], a[i]) { - chunk << a[i] + current << a[i] continue } - if chunk.len > 0 { - chunks << chunk + if current.len > 0 { + chunks << current } - chunk = [a[i]] + current = [a[i]] } - if chunk.len > 0 { - chunks << chunk + if current.len > 0 { + chunks << current } return chunks } @@ -667,19 +666,19 @@ fn swap_nonoverlapping[T](x_ &T, y_ &T, count int) { // The number of the elements copied is the minimum of the length of both arrays. // Returns the number of elements copied. pub fn copy[T](mut dst []T, src []T) int { - min := if dst.len < src.len { dst.len } else { src.len } - if min <= 0 { + min_len := if dst.len < src.len { dst.len } else { src.len } + if min_len <= 0 { return 0 } if can_copy_bits[T]() { - blen := min * isize(sizeof(T)) + blen := min_len * isize(sizeof(T)) unsafe { vmemmove(&T(dst.data), src.data, blen) } } else { - for i in 0 .. min { + for i in 0 .. min_len { dst[i] = src[i] } } - return min + return min_len } // can_copy_bits determines if T can be copied using `memcpy`. diff --git a/vlib/crypto/sha512/sha512.v b/vlib/crypto/sha512/sha512.v index cf4e20c8c..945c73283 100644 --- a/vlib/crypto/sha512/sha512.v +++ b/vlib/crypto/sha512/sha512.v @@ -283,9 +283,9 @@ pub fn sum384(data []u8) []u8 { mut d := new_digest(.sha384) d.write(data) or { panic(err) } sum := d.checksum() - mut sum384 := []u8{len: size384} - copy(mut sum384, sum[..size384]) - return sum384 + mut digest := []u8{len: size384} + copy(mut digest, sum[..size384]) + return digest } // sum512_224 returns the Sum512/224 checksum of the data. diff --git a/vlib/datatypes/doubly_linked_list.v b/vlib/datatypes/doubly_linked_list.v index d7f068829..77eb001d2 100644 --- a/vlib/datatypes/doubly_linked_list.v +++ b/vlib/datatypes/doubly_linked_list.v @@ -53,34 +53,34 @@ pub fn (list DoublyLinkedList[T]) last() !T { // push_back adds an element to the end of the linked list pub fn (mut list DoublyLinkedList[T]) push_back(item T) { - mut new_node := &DoublyListNode[T]{ + mut created := &DoublyListNode[T]{ data: item } if list.is_empty() { // first node case - list.head = new_node - list.tail = new_node + list.head = created + list.tail = created } else { - list.tail.next = new_node - new_node.prev = list.tail - list.tail = new_node + list.tail.next = created + created.prev = list.tail + list.tail = created } list.len += 1 } // push_front adds an element to the beginning of the linked list pub fn (mut list DoublyLinkedList[T]) push_front(item T) { - mut new_node := &DoublyListNode[T]{ + mut created := &DoublyListNode[T]{ data: item } if list.is_empty() { // first node case - list.head = new_node - list.tail = new_node + list.head = created + list.tail = created } else { - list.head.prev = new_node - new_node.next = list.head - list.head = new_node + list.head.prev = created + created.next = list.head + list.head = created } list.len += 1 } diff --git a/vlib/datatypes/linked_list.v b/vlib/datatypes/linked_list.v index e84f8182c..70effbdba 100644 --- a/vlib/datatypes/linked_list.v +++ b/vlib/datatypes/linked_list.v @@ -60,16 +60,16 @@ pub fn (list LinkedList[T]) index(idx int) !T { // push adds an element to the end of the linked list pub fn (mut list LinkedList[T]) push(item T) { - new_node := &ListNode[T]{ + created := &ListNode[T]{ data: item } if list.is_empty() { // first node case - list.head = new_node + list.head = created } else { - list.tail.next = new_node + list.tail.next = created } - list.tail = new_node + list.tail = created list.len += 1 } @@ -129,27 +129,27 @@ pub fn (mut list LinkedList[T]) insert(idx int, item T) ! { list.push(item) return } - mut new_node := &ListNode[T]{ + mut created := &ListNode[T]{ data: item } if list.is_empty() { - list.head = new_node - list.tail = new_node + list.head = created + list.tail = created } else { mut node := list.head if idx == 0 { // first node case - new_node.next = node - list.head = new_node + created.next = node + list.head = created } else { for i := 0; i < idx - 1; i++ { node = node.next } - new_node.next = node.next - node.next = new_node - if isnil(new_node.next) { - list.tail = new_node + created.next = node.next + node.next = created + if isnil(created.next) { + list.tail = created } } } diff --git a/vlib/gg/m4/matrix.v b/vlib/gg/m4/matrix.v index 5f1e720d8..e44a45779 100644 --- a/vlib/gg/m4/matrix.v +++ b/vlib/gg/m4/matrix.v @@ -455,7 +455,7 @@ pub fn det(x Mat4) f32 { pub fn (x Mat4) inverse() Mat4 { unsafe { mut t := [6]f32{} - mut det := f32(0) + mut inv_det := f32(0) // vfmt off a := x.f[0][0] @@ -520,9 +520,9 @@ pub fn (x Mat4) inverse() Mat4 { tmp := (a * dest.f[0][0] + b * dest.f[0][1] + c * dest.f[0][2] + d * dest.f[0][3]) if tmp != 0 { - det = f32(1.0) / tmp + inv_det = f32(1.0) / tmp } - return dest.mul_scalar(det) + return dest.mul_scalar(inv_det) } return zero_m4() } diff --git a/vlib/math/stats/stats.v b/vlib/math/stats/stats.v index 402a6b58d..0039d6233 100644 --- a/vlib/math/stats/stats.v +++ b/vlib/math/stats/stats.v @@ -104,13 +104,13 @@ pub fn mode[T](data []T) T { for v in data { freqs << freq(data, v) } - mut max := 0 + mut max_idx := 0 for i := 0; i < freqs.len; i++ { - if freqs[i] > freqs[max] { - max = i + if freqs[i] > freqs[max_idx] { + max_idx = i } } - return data[max] + return data[max_idx] } // rms, Root Mean Square, calculates the sqrt of the mean of the squares of the given input array @@ -295,11 +295,11 @@ pub fn tss_mean[T](data []T, mean T) T { if data.len == 0 { return T(0) } - mut tss := T(0) + mut sum_sq := T(0) for v in data { - tss += T((v - mean) * (v - mean)) + sum_sq += T((v - mean) * (v - mean)) } - return tss + return sum_sq } // min finds the minimum value from the dataset @@ -307,13 +307,13 @@ pub fn min[T](data []T) T { if data.len == 0 { return T(0) } - mut min := data[0] + mut lo := data[0] for v in data { - if v < min { - min = v + if v < lo { + lo = v } } - return min + return lo } // max finds the maximum value from the dataset @@ -321,13 +321,13 @@ pub fn max[T](data []T) T { if data.len == 0 { return T(0) } - mut max := data[0] + mut hi := data[0] for v in data { - if v > max { - max = v + if v > hi { + hi = v } } - return max + return hi } // minmax finds the minimum and maximum value from the dataset @@ -335,17 +335,17 @@ pub fn minmax[T](data []T) (T, T) { if data.len == 0 { return T(0), T(0) } - mut max := data[0] - mut min := data[0] + mut hi := data[0] + mut lo := data[0] for v in data[1..] { - if v > max { - max = v + if v > hi { + hi = v } - if v < min { - min = v + if v < lo { + lo = v } } - return min, max + return lo, hi } // min_index finds the first index of the minimum value @@ -353,15 +353,15 @@ pub fn min_index[T](data []T) int { if data.len == 0 { return 0 } - mut min := data[0] - mut min_index := 0 + mut lo := data[0] + mut lo_idx := 0 for i, v in data { - if v < min { - min = v - min_index = i + if v < lo { + lo = v + lo_idx = i } } - return min_index + return lo_idx } // max_index finds the first index of the maximum value @@ -369,15 +369,15 @@ pub fn max_index[T](data []T) int { if data.len == 0 { return 0 } - mut max := data[0] - mut max_index := 0 + mut hi := data[0] + mut hi_idx := 0 for i, v in data { - if v > max { - max = v - max_index = i + if v > hi { + hi = v + hi_idx = i } } - return max_index + return hi_idx } // minmax_index finds the first index of the minimum and maximum value @@ -385,21 +385,21 @@ pub fn minmax_index[T](data []T) (int, int) { if data.len == 0 { return 0, 0 } - mut min := data[0] - mut max := data[0] - mut min_index := 0 - mut max_index := 0 + mut lo := data[0] + mut hi := data[0] + mut lo_idx := 0 + mut hi_idx := 0 for i, v in data { - if v < min { - min = v - min_index = i + if v < lo { + lo = v + lo_idx = i } - if v > max { - max = v - max_index = i + if v > hi { + hi = v + hi_idx = i } } - return min_index, max_index + return lo_idx, hi_idx } // range calculates the difference between the min and max @@ -410,8 +410,8 @@ pub fn range[T](data []T) T { if data.len == 0 { return T(0) } - min, max := minmax[T](data) - return max - min + lo, hi := minmax[T](data) + return hi - lo } // covariance calculates directional association between datasets @@ -430,13 +430,13 @@ pub fn covariance_mean[T](data1 []T, data2 []T, mean1 T, mean2 T) T { if n == 0 { return T(0) } - mut covariance := T(0) + mut cov := T(0) for i in 0 .. n { delta1 := data1[i] - mean1 delta2 := data2[i] - mean2 - covariance += T((delta1 * delta2 - covariance) / (T(i) + T(1))) + cov += T((delta1 * delta2 - cov) / (T(i) + T(1))) } - return covariance + return cov } // lag1_autocorrelation_mean calculates the correlation between values that are one time period apart @@ -509,7 +509,7 @@ pub fn skew_mean_stddev[T](data []T, mean T, sd T) T { if data.len == 0 { return T(0) } - mut skew := T(0) // find the sum of the cubed deviations, normalized by the sd. + mut total := T(0) // find the sum of the cubed deviations, normalized by the sd. /* we use a recurrence relation to stably update a running value so * there aren't any large sums that can overflow @@ -517,9 +517,9 @@ pub fn skew_mean_stddev[T](data []T, mean T, sd T) T { for i, v in data { x := (v - mean) / sd x3 := x * x * x - skew += T((x3 - skew) / (T(i) + T(1))) + total += T((x3 - total) / (T(i) + T(1))) } - return skew + return total } // quantile calculates quantile points diff --git a/vlib/net/s3/client.v b/vlib/net/s3/client.v index becbd254b..4617fcf57 100644 --- a/vlib/net/s3/client.v +++ b/vlib/net/s3/client.v @@ -278,12 +278,12 @@ fn (c &Client) creds_for(bucket string) Credentials { if bucket == '' { return c.credentials } - mut copy := c.credentials - copy = Credentials{ - ...copy + mut dup := c.credentials + dup = Credentials{ + ...dup bucket: bucket } - return copy + return dup } // build_object_path produces the canonical URI path for a key. diff --git a/vlib/os/os.js.v b/vlib/os/os.js.v index 1bf13caa9..948e3d20c 100644 --- a/vlib/os/os.js.v +++ b/vlib/os/os.js.v @@ -129,15 +129,15 @@ pub fn join_path_single(base string, elem string) string { pub fn execute(cmd string) Result { mut exit_code := 0 - mut stdout := '' + mut out := '' #let commands = cmd.str.split(' '); #let output = $child_process.spawnSync(commands[0],commands.slice(1,commands.length)); #exit_code = new int(output.status) - #stdout = new string(output.stdout + '') + #out = new string(output.stdout + '') return Result{ exit_code: exit_code - output: stdout + output: out } } @@ -150,15 +150,15 @@ pub fn exec(args []string) Result { } } mut exit_code := 0 - mut stdout := '' + mut out := '' #let commands = args.arr.map((x) => x.valueOf() + ''); #let output = $child_process.spawnSync(commands[0], commands.slice(1, commands.length)); #exit_code = new int(output.status === null ? -1 : output.status) - #stdout = new string((output.stdout ? output.stdout + '' : '') + (output.stderr ? output.stderr + '' : '')) + #out = new string((output.stdout ? output.stdout + '' : '') + (output.stderr ? output.stderr + '' : '')) return Result{ exit_code: exit_code - output: stdout + output: out } } diff --git a/vlib/os/os.v b/vlib/os/os.v index e9c2ae27c..21972dc8c 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -996,9 +996,9 @@ pub fn data_dir() string { } home := home_dir() if home != '' { - dir := join_path(home, 'AppData', 'Local') - create_folder_when_it_does_not_exist(dir) - return dir + path := join_path(home, 'AppData', 'Local') + create_folder_when_it_does_not_exist(path) + return path } } return xdg_home_folder('XDG_DATA_HOME', '.local/share') diff --git a/vlib/os/os_js.js.v b/vlib/os/os_js.js.v index 0b3ee2236..624c133ba 100644 --- a/vlib/os/os_js.js.v +++ b/vlib/os/os_js.js.v @@ -35,17 +35,17 @@ struct PathKind { } fn kind_of_existing_path(path string) PathKind { - is_link := false - is_dir := false + link := false + directory := false $if js_node { - #is_link.val = $fs.existsSync(path.str) && $fs.lstatSync(path.str).isSymbolicLink() - #is_dir.val = $fs.existsSync(path,str) && $fs.lstatSync(path.str).isDirectory() + #link.val = $fs.existsSync(path.str) && $fs.lstatSync(path.str).isSymbolicLink() + #directory.val = $fs.existsSync(path,str) && $fs.lstatSync(path.str).isDirectory() } $else { _ = path } return PathKind{ - is_dir: is_dir - is_link: is_link + is_dir: directory + is_link: link } } diff --git a/vlib/os/os_windows.c.v b/vlib/os/os_windows.c.v index 43ca45ab6..7be784849 100644 --- a/vlib/os/os_windows.c.v +++ b/vlib/os/os_windows.c.v @@ -175,9 +175,9 @@ fn utf16_captured_output_to_string(raw string, little_endian bool) string { pairs := (raw.len - start) / 2 mut wide := []u16{len: pairs + 1, init: u16(0)} for idx := 0; idx < pairs; idx++ { - base := start + idx * 2 - b0 := u16(raw[base]) - b1 := u16(raw[base + 1]) + offset := start + idx * 2 + b0 := u16(raw[offset]) + b1 := u16(raw[offset + 1]) wide[idx] = if little_endian { b0 | (b1 << 8) } else { (b0 << 8) | b1 } } res := unsafe { string_from_wide2(wide.data, pairs) } @@ -734,23 +734,23 @@ pub fn uname() Uname { } pub fn hostname() !string { - hostname := [255]u16{} + buf := [255]u16{} size := u32(255) - res := C.GetComputerNameW(&hostname[0], voidptr(&size)) + res := C.GetComputerNameW(&buf[0], voidptr(&size)) if !res { return error(get_error_msg(int(C.GetLastError()))) } - return unsafe { string_from_wide(&hostname[0]) } + return unsafe { string_from_wide(&buf[0]) } } pub fn loginname() !string { - loginname := [255]u16{} + buf := [255]u16{} size := u32(255) - res := C.GetUserNameW(&loginname[0], voidptr(&size)) + res := C.GetUserNameW(&buf[0], voidptr(&size)) if !res { return error(get_error_msg(int(C.GetLastError()))) } - return unsafe { string_from_wide(&loginname[0]) } + return unsafe { string_from_wide(&buf[0]) } } // ensure_folder_is_writable checks that `folder` exists, and is writable to the process, by creating an empty file in it, then deleting it. diff --git a/vlib/sokol/sapp/sapp_egl_linux.v b/vlib/sokol/sapp/sapp_egl_linux.v index e1776269c..37ed627cf 100644 --- a/vlib/sokol/sapp/sapp_egl_linux.v +++ b/vlib/sokol/sapp/sapp_egl_linux.v @@ -4,7 +4,7 @@ module sapp // Shared between Wayland and X11 backends. fn sapp_egl_choose_config() EGLConfig { - sample_count := if g_sapp_state.desc.sample_count > 1 { + samples := if g_sapp_state.desc.sample_count > 1 { EGLint(g_sapp_state.desc.sample_count) } else { EGLint(0) @@ -32,7 +32,7 @@ fn sapp_egl_choose_config() EGLConfig { egl_sample_buffers, sample_buffers, egl_samples, - sample_count, + samples, egl_none, ]! @@ -61,7 +61,7 @@ fn sapp_egl_choose_config() EGLConfig { && C.eglGetConfigAttrib(g_sapp_state.egl.display, c, egl_depth_size, &d) != 0 && C.eglGetConfigAttrib(g_sapp_state.egl.display, c, egl_stencil_size, &s) != 0 && C.eglGetConfigAttrib(g_sapp_state.egl.display, c, egl_samples, &n) != 0 && r == 8 - && g == 8 && b == 8 && a == alpha_size && d == 24 && s == 8 && n == sample_count { + && g == 8 && b == 8 && a == alpha_size && d == 24 && s == 8 && n == samples { config = c break } diff --git a/vlib/sokol/sapp/sapp_wayland_linux.v b/vlib/sokol/sapp/sapp_wayland_linux.v index 1e4b9f298..20ba7d8c9 100644 --- a/vlib/sokol/sapp/sapp_wayland_linux.v +++ b/vlib/sokol/sapp/sapp_wayland_linux.v @@ -668,23 +668,23 @@ $if sokol_wayland ? { fn wl_xdg_toplevel_configure(data voidptr, xdg_toplevel &C.xdg_toplevel, width int, height int, states &C.wl_array) { // Iterate wl_array of uint32_t states - mut is_fullscreen := false - mut is_maximized := false + mut fullscreen := false + mut maximized := false if states.size > 0 { state_ptr := &u32(states.data) num_states := int(states.size / sizeof(u32)) for i in 0 .. num_states { s := unsafe { state_ptr[i] } if s == xdg_toplevel_state_fullscreen { - is_fullscreen = true + fullscreen = true } if s == xdg_toplevel_state_maximized { - is_maximized = true + maximized = true } } } - g_sapp_state.fullscreen = is_fullscreen - _ = is_maximized + g_sapp_state.fullscreen = fullscreen + _ = maximized if width > 0 && height > 0 { size_changed := g_sapp_state.wl.width != width || g_sapp_state.wl.height != height @@ -965,22 +965,22 @@ $if sokol_wayland ? { C.xdg_toplevel_set_min_size(g_sapp_state.wl.xdg_toplevel, 0, 0) C.xdg_toplevel_set_max_size(g_sapp_state.wl.xdg_toplevel, 0, 0) } else { - width := if g_sapp_state.window_width > 0 { + w := if g_sapp_state.window_width > 0 { g_sapp_state.window_width } else if g_sapp_state.wl.width > 0 { g_sapp_state.wl.width } else { fallback_default_window_width } - height := if g_sapp_state.window_height > 0 { + h := if g_sapp_state.window_height > 0 { g_sapp_state.window_height } else if g_sapp_state.wl.height > 0 { g_sapp_state.wl.height } else { fallback_default_window_height } - C.xdg_toplevel_set_min_size(g_sapp_state.wl.xdg_toplevel, i32(width), i32(height)) - C.xdg_toplevel_set_max_size(g_sapp_state.wl.xdg_toplevel, i32(width), i32(height)) + C.xdg_toplevel_set_min_size(g_sapp_state.wl.xdg_toplevel, i32(w), i32(h)) + C.xdg_toplevel_set_max_size(g_sapp_state.wl.xdg_toplevel, i32(w), i32(h)) } C.wl_surface_commit(g_sapp_state.wl.surface) if g_sapp_state.wl.display != unsafe { nil } { diff --git a/vlib/sokol/sapp/sapp_x11_linux.v b/vlib/sokol/sapp/sapp_x11_linux.v index 7c935071a..4c6f21439 100644 --- a/vlib/sokol/sapp/sapp_x11_linux.v +++ b/vlib/sokol/sapp/sapp_x11_linux.v @@ -927,14 +927,14 @@ fn x11_init_keytable() { C.XkbFreeKeyboard(desc, 0, x_true) // Fall back using traditional keysym lookup - mut width := 0 + mut syms_per_code := 0 keysyms := C.XGetKeyboardMapping(g_sapp_state.x11.display, u8(scancode_min), scancode_max - - scancode_min + 1, &width) + scancode_min + 1, &syms_per_code) for scancode in scancode_min .. scancode_max + 1 { if g_sapp_state.keycodes[scancode] == .invalid { - base := usize((scancode - scancode_min) * width) + base := usize((scancode - scancode_min) * syms_per_code) g_sapp_state.keycodes[scancode] = x11_translate_keysyms(unsafe { keysyms + base }, - width) + syms_per_code) } } C.XFree(keysyms) diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index d5484842d..7b643957f 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -671,11 +671,15 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { c.error('duplicate of an import symbol `${left.name}`', left.pos) } c.check_module_name_conflict(left.name, left.pos) - // Warn when a local variable shadows a function declaration (issue #22685) - mod_qualified := '${left.mod}.${left.name}' - if c.table.known_fn(mod_qualified) || c.table.known_fn(left.name) { - c.warn('variable `${left.name}` shadows a function declaration', - left.pos) + // Warn when a local variable shadows a function declaration (issue #22685). + // Skip in _test.v files: tests often use short fixtures like `fn a() {}` + // to verify function references in arrays/maps. + if !c.file.path.ends_with('_test.v') { + mod_qualified := '${left.mod}.${left.name}' + if c.table.known_fn(mod_qualified) || c.table.known_fn(left.name) { + c.warn('variable `${left.name}` shadows a function declaration', + left.pos) + } } } if node.op == .assign && left_type.has_flag(.option) && right is ast.UnsafeExpr -- 2.39.5