Skip to content

Commit 208855e

Browse files
committed
Add Luacheck integration
- Add CMake target that runs Luacheck - make luacheck - Add GH Actions workflow with Luacheck - Fixed warnings found by Luacheck Luacheck configuration file mostly based on recommendations from Lua style guide [1]. 1. https://www.tarantool.io/en/doc/latest/dev_guide/lua_style_guide/#luacheck Closes #75
1 parent bbf0eee commit 208855e

File tree

10 files changed

+89
-12
lines changed

10 files changed

+89
-12
lines changed

.github/workflows/check.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Static analysis
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
check:
9+
if: |
10+
github.event_name == 'push' ||
11+
github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
16+
- name: Setup Tarantool
17+
uses: tarantool/setup-tarantool@v1
18+
with:
19+
tarantool-version: '2.7'
20+
21+
- name: Setup luacheck
22+
run: tarantoolctl rocks install luacheck 0.25.0
23+
24+
- run: cmake -S . -B build
25+
26+
- name: Run luacheck
27+
run: make -C build luacheck

.luacheckrc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
std = "luajit"
2+
globals = {
3+
"box",
4+
}
5+
ignore = {
6+
-- Accessing an undefined field of a global variable <package>.
7+
"143/package",
8+
-- Unused argument <self>.
9+
"212/self",
10+
-- Redefining a local variable.
11+
"411",
12+
-- Redefining an argument.
13+
"412",
14+
-- Shadowing a local variable.
15+
"421",
16+
-- Shadowing an upvalue.
17+
"431",
18+
-- Shadowing an upvalue argument.
19+
"432",
20+
}
21+
22+
include_files = {
23+
"**/*.lua",
24+
}
25+
26+
exclude_files = {
27+
"build/**/*.lua",
28+
"test-run/**/*.lua",
29+
".rocks/**/*.lua",
30+
".git/**/*.lua",
31+
}

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ endif()
99
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
1010

1111
# Find Tarantool and Lua dependecies
12+
find_package(LuaCheck)
1213
set(TARANTOOL_FIND_REQUIRED ON)
1314
find_package(Tarantool)
1415
find_package(CyrusSASL)
@@ -85,3 +86,8 @@ if(POLICY CMP0037)
8586
cmake_policy(SET CMP0037 OLD) # don't blame `test` target name
8687
endif(POLICY CMP0037)
8788
add_subdirectory(test)
89+
90+
add_custom_target(luacheck
91+
COMMAND ${LUACHECK} ${PROJECT_SOURCE_DIR}
92+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
93+
)

cmake/FindLuaCheck.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
find_program(LUACHECK luacheck
2+
HINTS .rocks/
3+
PATH_SUFFIXES bin
4+
DOC "Lua linter"
5+
)
6+
7+
include(FindPackageHandleStandardArgs)
8+
find_package_handle_standard_args(LuaCheck
9+
REQUIRED_VARS LUACHECK
10+
)
11+
12+
mark_as_advanced(LUACHECK)

memcached/init.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local log = require('log')
88

99
local fmt = string.format
1010

11+
-- luacheck: ignore package
1112
local internal_so_path = package.search('memcached.internal')
1213
assert(internal_so_path, "Failed to find memcached/internal.so library")
1314
local memcached_internal = ffi.load(internal_so_path)
@@ -89,15 +90,15 @@ void memcached_handler (struct memcached_service *p, int fd);
8990
int memcached_setsockopt(int fd, const char *family, const char *type);
9091
]]
9192

92-
function startswith(str, start)
93+
local function startswith(str, start)
9394
return string.sub(str, 1, string.len(start)) == start
9495
end
9596

9697
local typetable = {
9798
name = {
9899
'string',
99100
function () return 'memcached' end,
100-
function (x) return true end,
101+
function () return true end,
101102
[[Name of memcached instance]]
102103
},
103104
uri = {
@@ -115,7 +116,7 @@ local typetable = {
115116
expire_enabled = {
116117
'boolean',
117118
function() return true end,
118-
function(x) return true end,
119+
function() return true end,
119120
[[configure availability of expiration daemon]]
120121
},
121122
expire_items_per_iter = {
@@ -217,6 +218,7 @@ local function config_initial(cfg)
217218
box.error{ reason = err }
218219
end
219220

221+
-- luacheck: no unused
220222
local function config_help()
221223
for k, v in pairs(typetable) do
222224
log.info('%s: %s', k, v[4])
@@ -310,14 +312,14 @@ local memcached_methods = {
310312
if (self.listener ~= nil) then
311313
self.listener:close()
312314
end
313-
local rc = memcached_internal.memcached_stop(self.service)
315+
memcached_internal.memcached_stop(self.service)
314316
self.status = STOPPED
315317
return self
316318
end,
317319
info = function (self)
318320
local stats = memcached_internal.memcached_get_stat(self.service)
319321
local retval = {}
320-
for k, v in pairs(stat_table) do
322+
for _, v in pairs(stat_table) do
321323
retval[v] = stats[0][v]
322324
end
323325
return retval

test/bench/memcached.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ box.cfg{
88

99
package.cpath = './?.so;' .. package.cpath
1010

11-
local inst = require('memcached').create('memcached', '0.0.0.0:11211', {
11+
require('memcached').create('memcached', '0.0.0.0:11211', {
1212
expire_full_scan_time = 120
1313
})
1414

test/binary/binary.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ box.cfg{
77

88
package.cpath = './?.so;' .. package.cpath
99

10-
local inst = require('memcached').create('memcached',
10+
require('memcached').create('memcached',
1111
os.getenv('LISTEN'):match(':(.*)'), {
1212
expire_full_scan_time = 1,
1313
})

test/capable/capable.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ box.cfg{
77

88
package.cpath = './?.so;' .. package.cpath
99

10-
local inst = require('memcached').create(
10+
require('memcached').create(
1111
'memcached',
1212
os.getenv('LISTEN'):match(':(.*)')
1313
)

test/sasl/sasl.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package.cpath = './?.so;' .. package.cpath
55
-- Manipulating environment variables --
66
--------------------------------------------------------------------------------
77
local ffi = require('ffi')
8-
local log = require('log')
98
local errno = require('errno')
109

1110
ffi.cdef[[
@@ -43,7 +42,7 @@ local env = setmetatable({}, {
4342
return ffi.string(var)
4443
end,
4544
__newindex = function(self, key, value)
46-
local rv = nil
45+
local rv
4746
if value ~= nil then
4847
rv = ffi.C.setenv(key, value, 1)
4948
else
@@ -75,7 +74,7 @@ local memcached = require('memcached')
7574
local listen_port = env['LISTEN']:match(':(.*)')
7675
local admin_port = env['ADMIN']
7776

78-
local inst = memcached.create('memcached', listen_port, {
77+
memcached.create('memcached', listen_port, {
7978
expire_full_scan_time = 1,
8079
sasl = true
8180
}):grant('guest')

test/text/text.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ box.cfg{
77

88
package.cpath = './?.so;' .. package.cpath
99

10-
local inst = require('memcached').create('memcached',
10+
require('memcached').create('memcached',
1111
os.getenv('LISTEN'):match(':(.*)'), {
1212
expire_full_scan_time = 1
1313
})

0 commit comments

Comments
 (0)