changeset 13925:a4c47203a9eb

various: Do not mutate loop variables as they are treated as const in Lua 5.5 Loop variables are treated as <const> in Lua 5.5 and can't be modified without declaring another local. Ref https://github.com/lua/lua/commit/b2f7b3b79f3117885b265575f6c5dbf934757797
author Kim Alvefur <zash@zash.se>
date Fri, 23 Feb 2024 18:44:13 +0100
parents 7fdf7645b0da
children 714695c910c7
files fallbacks/lxp.lua net/dns.lua net/http/files.lua net/server_select.lua plugins/mod_storage_xep0227.lua prosodyctl util/debug.lua util/http.lua util/pluginloader.lua util/termcolours.lua
diffstat 10 files changed, 26 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/fallbacks/lxp.lua	Tue Aug 05 22:35:01 2025 +0200
+++ b/fallbacks/lxp.lua	Fri Feb 23 18:44:13 2024 +0100
@@ -86,9 +86,9 @@
 		local newattr, n = {}, 0;
 		for k,v in pairs(attr) do
 			n = n+1;
-			k = apply_ns(k);
-			newattr[n] = k;
-			newattr[k] = v;
+			local k_ = apply_ns(k);
+			newattr[n] = k_;
+			newattr[k_] = v;
 		end
 		tag = apply_ns(tag, true);
 		ns[0] = tag;
--- a/net/dns.lua	Tue Aug 05 22:35:01 2025 +0200
+++ b/net/dns.lua	Fri Feb 23 18:44:13 2024 +0100
@@ -701,10 +701,10 @@
 		local resolv_conf = io.open("/etc/resolv.conf");
 		if resolv_conf then
 			for line in resolv_conf:lines() do
-				line = line:gsub("#.*$", "")
+				local nameserver = line:gsub("#.*$", "")
 					:match('^%s*nameserver%s+([%x:%.]*%%?%S*)%s*$');
-				if line then
-					local ip = new_ip(line);
+				if nameserver then
+					local ip = new_ip(nameserver);
 					if ip then
 						self:addnameserver(ip.addr);
 					end
--- a/net/http/files.lua	Tue Aug 05 22:35:01 2025 +0200
+++ b/net/http/files.lua	Fri Feb 23 18:44:13 2024 +0100
@@ -29,8 +29,8 @@
 	local out = {};
 
 	local c = 0;
-	for component in path:gmatch("([^/]+)") do
-		component = urldecode(component);
+	for urlcomponent in path:gmatch("([^/]+)") do
+		local component = urldecode(urlcomponent);
 		if component:find(forbidden_chars_pattern) then
 			return nil;
 		elseif component == ".." then
--- a/net/server_select.lua	Tue Aug 05 22:35:01 2025 +0200
+++ b/net/server_select.lua	Fri Feb 23 18:44:13 2024 +0100
@@ -620,6 +620,7 @@
 					_sendlistlen = ( wrote and removesocket( _sendlist, client, _sendlistlen ) ) or _sendlistlen
 					_readlistlen = ( read and removesocket( _readlist, client, _readlistlen ) ) or _readlistlen
 					read, wrote = nil, nil
+					local _
 					_, err = client:dohandshake( )
 					if not err then
 						out_put( "server.lua: ssl handshake done" )
--- a/plugins/mod_storage_xep0227.lua	Tue Aug 05 22:35:01 2025 +0200
+++ b/plugins/mod_storage_xep0227.lua	Fri Feb 23 18:44:13 2024 +0100
@@ -251,10 +251,10 @@
 				usere:add_child(roster);
 				for contact_jid, item in pairs(data) do
 					if contact_jid ~= false then
-						contact_jid = jid_bare(jid_prep(contact_jid));
-						if contact_jid ~= user_jid then -- Skip self-contacts
+						local bare_contact_jid = jid_bare(jid_prep(contact_jid));
+						if bare_contact_jid ~= user_jid then -- Skip self-contacts
 							roster:tag("item", {
-								jid = contact_jid,
+								jid = bare_contact_jid,
 								subscription = item.subscription,
 								ask = item.ask,
 								name = item.name,
--- a/prosodyctl	Tue Aug 05 22:35:01 2025 +0200
+++ b/prosodyctl	Fri Feb 23 18:44:13 2024 +0100
@@ -461,15 +461,15 @@
 		local version_field = alternate_version_fields[name] or "_VERSION";
 		if type(module) == "table" and rawget(module, version_field)
 			and name ~= "_G" and not name:match("%.") then
-			name = friendly_names[name] or name;
-			if #name > longest_name then
-				longest_name = #name;
+			local friendly_name = friendly_names[name] or name;
+			if #friendly_name > longest_name then
+				longest_name = #friendly_name;
 			end
 			local mod_version = module[version_field];
-			if tostring(mod_version):sub(1, #name+1) == name .. " " then
-				mod_version = mod_version:sub(#name+2);
+			if tostring(mod_version):sub(1, #friendly_name+1) == friendly_name .. " " then
+				mod_version = mod_version:sub(#friendly_name+2);
 			end
-			module_versions[name] = mod_version;
+			module_versions[friendly_name] = mod_version;
 		end
 	end
 	if lunbound then
--- a/util/debug.lua	Tue Aug 05 22:35:01 2025 +0200
+++ b/util/debug.lua	Fri Feb 23 18:44:13 2024 +0100
@@ -145,7 +145,8 @@
 	local last_source_desc;
 
 	local lines = {};
-	for nlevel, current_level in ipairs(levels) do
+	for n, current_level in ipairs(levels) do
+		local nlevel = n-1;
 		local info = current_level.info;
 		local line;
 		local func_type = info.namewhat.." ";
@@ -171,7 +172,6 @@
 			last_source_desc = source_desc;
 			table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
 		end
-		nlevel = nlevel-1;
 		table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
 		local npadding = (" "):rep(#tostring(nlevel));
 		if current_level.locals then
--- a/util/http.lua	Tue Aug 05 22:35:01 2025 +0200
+++ b/util/http.lua	Fri Feb 23 18:44:13 2024 +0100
@@ -45,8 +45,8 @@
 local function formdecode(s)
 	if not s:match("=") then return urldecode(s); end
 	local r = {};
-	for k, v in s:gmatch("([^=&]*)=([^&]*)") do
-		k, v = k:gsub("%+", "%%20"), v:gsub("%+", "%%20");
+	for uk, uv in s:gmatch("([^=&]*)=([^&]*)") do
+		local k, v = uk:gsub("%+", "%%20"), uv:gsub("%+", "%%20");
 		k, v = urldecode(k), urldecode(v);
 		t_insert(r, { name = k, value = v });
 		r[k] = v;
--- a/util/pluginloader.lua	Tue Aug 05 22:35:01 2025 +0200
+++ b/util/pluginloader.lua	Fri Feb 23 18:44:13 2024 +0100
@@ -10,10 +10,9 @@
 local dir_sep, path_sep = package.config:match("^(%S+)%s(%S+)");
 local lua_version = _VERSION:match(" (.+)$");
 local plugin_dir = {};
-for path in (CFG_PLUGINDIR or "./plugins/"):gsub("[/\\]", dir_sep):gmatch("[^"..path_sep.."]+") do
-	path = path..dir_sep; -- add path separator to path end
-	path = path:gsub(dir_sep..dir_sep.."+", dir_sep); -- coalesce multiple separators
-	plugin_dir[#plugin_dir + 1] = path;
+for path in (CFG_PLUGINDIR or "./plugins/"):gsub("[/\\]", dir_sep):gmatch("[^" .. path_sep .. "]+") do
+	plugin_dir[#plugin_dir + 1] = (path .. dir_sep) -- add path separator to path end
+		:gsub(dir_sep .. dir_sep .. "+", dir_sep); -- coalesce multiple separators
 end
 
 local io_open = io.open;
--- a/util/termcolours.lua	Tue Aug 05 22:35:01 2025 +0200
+++ b/util/termcolours.lua	Fri Feb 23 18:44:13 2024 +0100
@@ -95,8 +95,7 @@
 }
 for colorname, rgb in pairs(csscolors) do
 	stylemap[colorname] = stylemap[colorname] or stylemap[rgb];
-	colorname, rgb = colorname .. " background", rgb .. " background"
-	stylemap[colorname] = stylemap[colorname] or stylemap[rgb];
+	stylemap[colorname .. " background"] = stylemap[colorname .. " background"] or stylemap[rgb .. " background"];
 end
 
 local function getstyle(...)