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

@ -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

@ -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

@ -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)