Merge pull request #51 from idbrii/lint

Add luacheck and fix bugs it found
This commit is contained in:
Max Cahill 2022-03-07 10:14:08 +11:00 committed by GitHub
commit 4b0e0c7ec7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 88 additions and 24 deletions

26
.github/workflows/luacheck.yml vendored Normal file
View File

@ -0,0 +1,26 @@
name: Linting
on: [push, pull_request]
jobs:
luacheck:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
# Subdirectory to avoid linting luarocks code. Use same dir as testy.
path: batteries
- name: Setup Lua
uses: leafo/gh-actions-lua@v8
with:
luaVersion: 5.4
- name: Setup Lua Rocks
uses: leafo/gh-actions-luarocks@v4
- name: Setup luacheck
run: luarocks install luacheck
- name: Run Code Linter
run: |
cd batteries
luacheck .

33
.luacheckrc Normal file
View File

@ -0,0 +1,33 @@
return {
std = "lua51+love",
ignore = {
"211", -- Unused local variable.
"212/self", -- Unused argument self.
"213", -- Unused loop variable.
"631", -- Line is too long.
},
files = {
["tests.lua"] = {
ignore = {
"211", -- Unused local variable. (testy will find these local functions)
},
},
["assert.lua"] = {
ignore = {
"121", -- Setting a read-only global variable. (we clobber assert)
},
},
["init.lua"] = {
ignore = {
"111", -- Setting an undefined global variable. (batteries and ripairs)
"121", -- Setting a read-only global variable. (we clobber assert)
"143", -- Accessing an undefined field of a global variable. (we use tablex as table)
},
},
["sort.lua"] = {
ignore = {
"142", -- Setting an undefined field of a global variable. (inside export)
},
},
}
}

View File

@ -75,7 +75,7 @@ end
--replace everything in assert with nop functions that just return their second argument, for near-zero overhead on release --replace everything in assert with nop functions that just return their second argument, for near-zero overhead on release
function assert:nop() function assert:nop()
local nop = function(self, a) local nop = function(_, a)
return a return a
end end
setmetatable(self, { setmetatable(self, {

View File

@ -1,12 +1,12 @@
--[[ --[[
simple kernel for async tasks running in the background simple kernel for async tasks running in the background
can "stall" a task by yielding the string "stall" can "stall" a task by yielding the string "stall"
this will suspend the coroutine until the rest of this will suspend the coroutine until the rest of
the queue has been processed or stalled the queue has been processed or stalled
and can early-out update_for_time and can early-out update_for_time
todo: todo:
multiple types of callbacks multiple types of callbacks
finish, error, step finish, error, step
getting a reference to the task for manipulation getting a reference to the task for manipulation

View File

@ -136,7 +136,7 @@ function colour.rgb_to_hsl(r, g, b)
local l, d = max + min, max - min local l, d = max + min, max - min
local s = d / (l > 1 and (2 - l) or l) local s = d / (l > 1 and (2 - l) or l)
l = l / 2 l = l / 2
local h = nil --depends on below local h --depends on below
if max == r then if max == r then
h = (g - b) / d h = (g - b) / d
if g < b then h = h + 6 end if g < b then h = h + 6 end
@ -145,6 +145,7 @@ function colour.rgb_to_hsl(r, g, b)
else else
h = (r - g) / d + 4 h = (r - g) / d + 4
end end
assert(h)
return h / 6, s, l return h / 6, s, l
end end

View File

@ -209,7 +209,7 @@ end
function functional.stitch(t, f) function functional.stitch(t, f)
local result = {} local result = {}
for i, v in ipairs(t) do for i, v in ipairs(t) do
local v = f(v, i) v = f(v, i)
if v ~= nil then if v ~= nil then
if type(v) == "table" then if type(v) == "table" then
for _, e in ipairs(v) do for _, e in ipairs(v) do

View File

@ -186,7 +186,7 @@ function intersect.line_line_collide(a_start, a_end, a_rad, b_start, b_end, b_ra
local numerb = dx1 * dyab - dy1 * dxab local numerb = dx1 * dyab - dy1 * dxab
--check coincident lines --check coincident lines
local intersected = "none" local intersected
if if
math.abs(numera) < COLLIDE_EPS and math.abs(numera) < COLLIDE_EPS and
math.abs(numerb) < COLLIDE_EPS and math.abs(numerb) < COLLIDE_EPS and
@ -215,6 +215,7 @@ function intersect.line_line_collide(a_start, a_end, a_rad, b_start, b_end, b_ra
end end
end end
end end
assert(intersected)
if intersected == "both" then if intersected == "both" then
--simply displace along A normal --simply displace along A normal
@ -454,11 +455,11 @@ function intersect.point_aabb_collide(a, b_pos, b_hs, into)
end end
function intersect.circle_aabb_overlap(a, a_rad, b_pos, b_hs) function intersect.circle_aabb_overlap(a, a_rad, b_pos, b_hs)
return intersect.aabb_circle_overlap(b_pos, b_pos, a, a_rad) return intersect.aabb_circle_overlap(b_pos, b_hs, a, a_rad)
end end
function intersect.circle_aabb_collide(a, a_rad, b_pos, b_hs, into) function intersect.circle_aabb_collide(a, a_rad, b_pos, b_hs, into)
return intersect.reverse_msv(intersect.aabb_circle_collide(b_pos, b_pos, a, a_rad, into)) return intersect.reverse_msv(intersect.aabb_circle_collide(b_pos, b_hs, a, a_rad, into))
end end
function intersect.circle_line_collide(a, a_rad, b_start, b_end, b_rad, into) function intersect.circle_line_collide(a, a_rad, b_start, b_end, b_rad, into)

View File

@ -60,7 +60,7 @@ function pretty._process(input, config, processing_state)
--pull out config --pull out config
config = table.overlay({}, pretty.default_config, config or {}) config = table.overlay({}, pretty.default_config, config or {})
local per_line = config.per_line local per_line = config.per_line
local depth = config.depth local depth = config.depth
local indent = config.indent local indent = config.indent
@ -153,7 +153,6 @@ function pretty._process(input, config, processing_state)
end end
if break_next then if break_next then
table.insert(line_chunks, table.remove(chunks, 1)) table.insert(line_chunks, table.remove(chunks, 1))
break_next = false
end end
end end
chunks = line_chunks chunks = line_chunks

View File

@ -1,5 +1,8 @@
--[[ --[[
set type with appropriate operations set type with appropriate operations
NOTE: This is actually a unique list (ordered set). So it's more than just
a table with keys for values.
]] ]]
local path = (...):gsub("set", "") local path = (...):gsub("set", "")

View File

@ -56,8 +56,8 @@ function sort._merge(array, workspace, low, middle, high, less)
local i, j, k local i, j, k
i = 1 i = 1
-- copy first half of array to auxiliary array -- copy first half of array to auxiliary array
for j = low, middle do for w = low, middle do
workspace[i] = array[j] workspace[i] = array[w]
i = i + 1 i = i + 1
end end
-- sieve through -- sieve through
@ -78,8 +78,8 @@ function sort._merge(array, workspace, low, middle, high, less)
k = k + 1 k = k + 1
end end
-- copy back any remaining elements of first half -- copy back any remaining elements of first half
for k = k, j - 1 do for w = k, j - 1 do
array[k] = workspace[i] array[w] = workspace[i]
i = i + 1 i = i + 1
end end
end end
@ -121,7 +121,8 @@ end
function sort.stable_sort(array, less) function sort.stable_sort(array, less)
--setup --setup
local trivial, n, less = sort._sort_setup(array, less) local trivial, n
trivial, n, less = sort._sort_setup(array, less)
if not trivial then if not trivial then
--temp storage; allocate ahead of time --temp storage; allocate ahead of time
local workspace = {} local workspace = {}
@ -135,7 +136,8 @@ end
function sort.insertion_sort(array, less) function sort.insertion_sort(array, less)
--setup --setup
local trivial, n, less = sort._sort_setup(array, less) local trivial, n
trivial, n, less = sort._sort_setup(array, less)
if not trivial then if not trivial then
sort._insertion_sort_impl(array, 1, n, less) sort._insertion_sort_impl(array, 1, n, less)
end end

View File

@ -115,7 +115,7 @@ end
--hard-replace a state table --hard-replace a state table
-- if we're replacing the current state, -- if we're replacing the current state,
-- exit is called on the old state and enter is called on the new state -- exit is called on the old state and enter is called on the new state
-- mask_transitions can be used to prevent this if you need to -- mask_transitions can be used to prevent this if you need to
function state_machine:replace_state(name, state, mask_transitions) function state_machine:replace_state(name, state, mask_transitions)
local do_transitions = not mask_transitions and self:in_state(name) local do_transitions = not mask_transitions and self:in_state(name)
if do_transitions then if do_transitions then

View File

@ -171,9 +171,7 @@ function stringx.deindent(s, keep_trailing_empty)
--split along newlines --split along newlines
local lines = stringx.split(s, newline) local lines = stringx.split(s, newline)
--detect and strip any leading blank lines --detect and strip any leading blank lines
local leading_newline = false
while lines[1] == "" do while lines[1] == "" do
leading_newline = true
table.remove(lines, 1) table.remove(lines, 1)
end end
@ -249,6 +247,7 @@ end
--check if a given string starts with another --check if a given string starts with another
--(without garbage) --(without garbage)
--Using loops is actually faster than string.find!
function stringx.starts_with(s, prefix) function stringx.starts_with(s, prefix)
for i = 1, #prefix do for i = 1, #prefix do
if s:byte(i) ~= prefix:byte(i) then if s:byte(i) ~= prefix:byte(i) then

View File

@ -282,7 +282,7 @@ function tablex.append_inplace(t1, t2, ...)
table.insert(t1, v) table.insert(t1, v)
end end
if ... then if ... then
return table.append_inplace(t1, ...) return tablex.append_inplace(t1, ...)
end end
return t1 return t1
end end
@ -398,8 +398,8 @@ function tablex.collapse(t)
local r = {} local r = {}
for _, v in ipairs(t) do for _, v in ipairs(t) do
if type(v) == "table" then if type(v) == "table" then
for _, v in ipairs(v) do for _, w in ipairs(v) do
table.insert(r, v) table.insert(r, w)
end end
else else
table.insert(r, v) table.insert(r, v)

View File

@ -266,7 +266,7 @@ end
vec2.rot180_inplace = vec2.inverse_inplace --alias vec2.rot180_inplace = vec2.inverse_inplace --alias
--get the angle of this vector relative to (1, 0) --get the angle of this vector relative to (1, 0)
function vec2:angle() function vec2:angle()
return math.atan2(self.y, self.x) return math.atan2(self.y, self.x)
end end

View File

@ -343,7 +343,7 @@ local _euler_macro = {
} }
function vec3:rotate_euleri(angle_x_axis, angle_y_axis, angle_z_axis) function vec3:rotate_euleri(angle_x_axis, angle_y_axis, angle_z_axis)
for i, swizzle in ipairs(_euler_macro) do for i, swizzle in ipairs(_euler_macro) do
local angle = local angle =
i == 1 and angle_x_axis i == 1 and angle_x_axis
or i == 2 and angle_y_axis or i == 2 and angle_y_axis
or i == 3 and angle_z_axis or i == 3 and angle_z_axis