luacheck
luacheck copied to clipboard
Determine whether a method/function is a pure setter, and trigger “mutated but never accessed” warning
Here is a minimal showcase of my problem:
local a = {};
a.set_4_into_2 = function(t)
t[2] = 4;
end
-- a[2] = 4;
-- a:set_4_into_2();
If you uncomment the first comment, you get a “a.lua:1:7: variable a is mutated but never accessed” warning, but if you uncomment the second one luacheck
says that everything is fine.
The method in question could be determined to only be a setter through analysis, or (simpler for a first step) could require to be annotated by a hint in order to consider it a setter.
So the idea is that calling a pure setter method should not mark the table as accessed, but instead it should be considered a mutation. This makes sense, although I'm not sure how often this comes up. Could also work with setters that accept values as arguments:
local a = {}
function a:set_k(v)
self.k = v
end
a:set_k(123456) -- a is still reported as mutated but never accessed