From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp49.i.mail.ru (smtp49.i.mail.ru [94.100.177.109]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id AC8564696C6 for ; Tue, 21 Apr 2020 17:02:32 +0300 (MSK) From: sergeyb@tarantool.org Date: Tue, 21 Apr 2020 17:00:22 +0300 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH v4 5/10] Fix luacheck warnings in src/box/lua/ List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, v.shpilevoy@tarantool.org Cc: o.piskunov@tarantool.org From: Sergey Bronnikov Closes #4681 Reviewed-by: Vladislav Shpilevoy Co-authored-by: Vladislav Shpilevoy --- .luacheckrc | 8 ++++++++ src/box/lua/console.lua | 6 +++--- src/box/lua/feedback_daemon.lua | 2 +- src/box/lua/key_def.lua | 2 +- src/box/lua/load_cfg.lua | 25 +++++++++++------------ src/box/lua/net_box.lua | 26 ++++++++---------------- src/box/lua/schema.lua | 35 ++++++++++++++++----------------- src/box/lua/tuple.lua | 8 +++----- src/box/lua/upgrade.lua | 19 +++++++++--------- 9 files changed, 62 insertions(+), 69 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 60aedc842..64692b27c 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -25,7 +25,15 @@ exclude_files = { ".git/**/*.lua", } +files["**/*.lua"] = { + globals = {"box", "_TARANTOOL", "help", "tutorial"}, + ignore = {"212/self", "122"} +} files["extra/dist/tarantoolctl.in"] = {ignore = {"212/self", "122", "431"}} files["src/lua/swim.lua"] = {ignore = {"431"}} files["src/lua/fio.lua"] = {ignore = {"231"}} files["src/lua/init.lua"] = {globals = {"dostring"}} +files["src/box/lua/console.lua"] = {ignore = {"212"}} +files["src/box/lua/load_cfg.lua"] = {ignore = {"542"}} +files["src/box/lua/net_box.lua"] = {ignore = {"431", "432", "231", "411", "212"}} +files["src/box/lua/schema.lua"] = {ignore = {"431", "432", "542", "212"}} diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua index 2add9a79d..5fd8b5e4e 100644 --- a/src/box/lua/console.lua +++ b/src/box/lua/console.lua @@ -465,7 +465,7 @@ local text_connection_mt = { -- Make sure it is exactly "\set output" command. if operators[items[1]] == set_param and param_handlers[items[2]] == set_output then - local err, fmt, opts = parse_output(items[3]) + local err, fmt = parse_output(items[3]) if not err then self.fmt = fmt self.eos = output_eos[fmt] @@ -489,7 +489,7 @@ local text_connection_mt = { break end if fmt == "yaml" then - local handle, prefix = yaml.decode(rc, {tag_only = true}) + local handle = yaml.decode(rc, {tag_only = true}) if not handle then -- Can not fail - tags are encoded with no -- user participation and are correct always. @@ -869,7 +869,7 @@ local function listen(uri) host = u.host port = u.service or 3313 end - local s, addr = socket.tcp_server(host, port, { handler = client_handler, + local s = socket.tcp_server(host, port, { handler = client_handler, name = 'console'}) if not s then error(string.format('failed to create server %s:%s: %s', diff --git a/src/box/lua/feedback_daemon.lua b/src/box/lua/feedback_daemon.lua index ad71a3fe2..06bf3f281 100644 --- a/src/box/lua/feedback_daemon.lua +++ b/src/box/lua/feedback_daemon.lua @@ -60,7 +60,7 @@ local function guard_loop(self) self.fiber = fiber.create(feedback_loop, self) log.verbose("%s restarted", PREFIX) end - local st, err = pcall(fiber.sleep, self.interval) + local st = pcall(fiber.sleep, self.interval) if not st then -- fiber was cancelled break diff --git a/src/box/lua/key_def.lua b/src/box/lua/key_def.lua index 586005709..a1b61441e 100644 --- a/src/box/lua/key_def.lua +++ b/src/box/lua/key_def.lua @@ -15,5 +15,5 @@ ffi.metatype(key_def_t, { __index = function(self, key) return methods[key] end, - __tostring = function(key_def) return "" end, + __tostring = function() return "" end, }) diff --git a/src/box/lua/load_cfg.lua b/src/box/lua/load_cfg.lua index b671eb7a2..0926c7380 100644 --- a/src/box/lua/load_cfg.lua +++ b/src/box/lua/load_cfg.lua @@ -22,7 +22,7 @@ local function locked(f) end -- all available options -local default_cfg = { +local default_config = { listen = nil, memtx_memory = 256 * 1024 *1024, strip_core = true, @@ -87,7 +87,7 @@ local default_cfg = { -- types of available options -- could be comma separated lua types or 'any' if any type is allowed -local template_cfg = { +local template_config = { listen = 'string, number', memtx_memory = 'number', strip_core = 'boolean', @@ -170,7 +170,7 @@ local function normalize_uri_list(port_list) end -- options that require special handling -local modify_cfg = { +local modify_config = { listen = normalize_uri, replication = normalize_uri_list, } @@ -335,7 +335,7 @@ end -- value of the old option, value of the new if present. It -- returns two values - value to replace the old option and to -- replace the new one. -local translate_cfg = { +local translate_config = { snapshot_count = {'checkpoint_count'}, snapshot_period = {'checkpoint_interval'}, slab_alloc_arena = {'memtx_memory', function(old) @@ -430,7 +430,6 @@ local function prepare_cfg(cfg, default_cfg, template_cfg, modify_cfg, prefix) else local good_types = string.gsub(template_cfg[k], ' ', ''); if (string.find(',' .. good_types .. ',', ',' .. type(v) .. ',') == nil) then - good_types = string.gsub(good_types, ',', ', '); box.error(box.error.CFG, readable_name, "should be one of types ".. template_cfg[k]) end @@ -473,8 +472,8 @@ local function compare_cfg(cfg1, cfg2) end local function reload_cfg(oldcfg, cfg) - cfg = upgrade_cfg(cfg, translate_cfg) - local newcfg = prepare_cfg(cfg, default_cfg, template_cfg, modify_cfg) + cfg = upgrade_cfg(cfg, translate_config) + local newcfg = prepare_cfg(cfg, default_config, template_config, modify_config) local ordered_cfg = {} -- iterate over original table because prepare_cfg() may store NILs for key, val in pairs(cfg) do @@ -527,16 +526,16 @@ for k, v in pairs(box) do end setmetatable(box, { - __index = function(table, index) + __index = function() error(debug.traceback("Please call box.cfg{} first")) error("Please call box.cfg{} first") end }) local function load_cfg(cfg) - cfg = upgrade_cfg(cfg, translate_cfg) - cfg = prepare_cfg(cfg, default_cfg, template_cfg, modify_cfg) - apply_default_cfg(cfg, default_cfg); + cfg = upgrade_cfg(cfg, translate_config) + cfg = prepare_cfg(cfg, default_config, template_config, modify_config) + apply_default_cfg(cfg, default_config); -- Save new box.cfg box.cfg = cfg if not pcall(private.cfg_check) then @@ -551,7 +550,7 @@ local function load_cfg(cfg) box_configured = nil box.cfg = setmetatable(cfg, { - __newindex = function(table, index) + __newindex = function() error('Attempt to modify a read-only table') end, __call = locked(reload_cfg), @@ -563,7 +562,7 @@ local function load_cfg(cfg) if not dynamic_cfg_skip_at_load[key] then fun() end - if not compare_cfg(val, default_cfg[key]) then + if not compare_cfg(val, default_config[key]) then if log_cfg_option[key] ~= nil then val = log_cfg_option[key](val) end diff --git a/src/box/lua/net_box.lua b/src/box/lua/net_box.lua index 07fa54c38..1e351bb31 100644 --- a/src/box/lua/net_box.lua +++ b/src/box/lua/net_box.lua @@ -38,10 +38,6 @@ local IPROTO_STATUS_KEY = 0x00 local IPROTO_ERRNO_MASK = 0x7FFF local IPROTO_SYNC_KEY = 0x01 local IPROTO_SCHEMA_VERSION_KEY = 0x05 -local IPROTO_METADATA_KEY = 0x32 -local IPROTO_SQL_INFO_KEY = 0x42 -local SQL_INFO_ROW_COUNT_KEY = 0 -local IPROTO_FIELD_NAME_KEY = 0 local IPROTO_DATA_KEY = 0x30 local IPROTO_ERROR_KEY = 0x31 local IPROTO_ERROR_STACK = 0x52 @@ -155,7 +151,7 @@ local function next_id(id) return band(id + 1, 0x7FFFFFFF) end -- @retval two non-nils A connected socket and a decoded greeting. -- local function establish_connection(host, port, timeout) - local timeout = timeout or DEFAULT_CONNECT_TIMEOUT + timeout = timeout or DEFAULT_CONNECT_TIMEOUT local begin = fiber.clock() local s = socket.tcp_connect(host, port, timeout) if not s then @@ -425,7 +421,7 @@ local function create_transport(host, port, user, password, callback, state = new_state last_errno = new_errno last_error = new_error - callback('state_changed', new_state, new_errno, new_error) + callback('state_changed', new_state, new_error) state_cond:broadcast() if state == 'error' or state == 'error_reconnect' or state == 'closed' then @@ -647,7 +643,7 @@ local function create_transport(host, port, user, password, callback, local function send_and_recv_iproto(timeout) local data_len = recv_buf.wpos - recv_buf.rpos - local required = 0 + local required if data_len < 5 then required = 5 else @@ -711,7 +707,8 @@ local function create_transport(host, port, user, password, callback, "please use require('console').connect() instead") local setup_delimiter = 'require("console").delimiter("$EOF$")\n' method_encoder.inject(send_buf, nil, setup_delimiter) - local err, response = send_and_recv_console() + local response + err, response = send_and_recv_console() if err then return error_sm(err, response) elseif response ~= '---\n...\n' then @@ -729,7 +726,6 @@ local function create_transport(host, port, user, password, callback, end console_sm = function(rid) - local delim = '\n...\n' local err, response = send_and_recv_console() if err then return error_sm(err, response) @@ -803,8 +799,7 @@ local function create_transport(host, port, user, password, callback, local status = hdr[IPROTO_STATUS_KEY] local response_schema_version = hdr[IPROTO_SCHEMA_VERSION_KEY] if status ~= 0 then - local body - body, body_end = decode(body_rpos) + local body = decode(body_rpos) return error_sm(E_NO_CONNECTION, body[IPROTO_ERROR_KEY]) end if schema_version == nil then @@ -813,8 +808,7 @@ local function create_transport(host, port, user, password, callback, -- schema changed while fetching schema; restart loader return iproto_schema_sm() end - local body - body, body_end = decode(body_rpos) + local body = decode(body_rpos) response[id] = body[IPROTO_DATA_KEY] end until response[select1_id] and response[select2_id] and @@ -830,14 +824,11 @@ local function create_transport(host, port, user, password, callback, local err, hdr, body_rpos, body_end = send_and_recv_iproto() if err then return error_sm(err, hdr) end dispatch_response_iproto(hdr, body_rpos, body_end) - local status = hdr[IPROTO_STATUS_KEY] local response_schema_version = hdr[IPROTO_SCHEMA_VERSION_KEY] if response_schema_version > 0 and response_schema_version ~= schema_version then -- schema_version has been changed - start to load a new version. -- Sic: self.schema_version will be updated only after reload. - local body - body, body_end = decode(body_rpos) set_state('fetch_schema') return iproto_schema_sm(schema_version) end @@ -954,7 +945,7 @@ local function new_sm(host, port, opts, connection, greeting) local remote = {host = host, port = port, opts = opts, state = 'initial'} local function callback(what, ...) if what == 'state_changed' then - local state, errno, err = ... + local state, err = ... local was_connected = remote._is_connected if state == 'active' then if not was_connected then @@ -1601,7 +1592,6 @@ this_module.self = { rollback() return box.error() -- re-throw end - local result if obj ~= nil then return handle_eval_result(pcall(proc, obj, unpack(args))) else diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua index 85fcca562..de47b807e 100644 --- a/src/box/lua/schema.lua +++ b/src/box/lua/schema.lua @@ -2,7 +2,6 @@ -- local ffi = require('ffi') local msgpack = require('msgpack') -local msgpackffi = require('msgpackffi') local fun = require('fun') local log = require('log') local fio = require('fio') @@ -210,7 +209,7 @@ local function revoke_object_privs(object_type, object_id) local _vpriv = box.space[box.schema.VPRIV_ID] local _priv = box.space[box.schema.PRIV_ID] local privs = _vpriv.index.object:select{object_type, object_id} - for k, tuple in pairs(privs) do + for _, tuple in pairs(privs) do local uid = tuple.grantee _priv:delete{uid, object_type, object_id} end @@ -274,7 +273,6 @@ local function check_param_table(table, template) local haystack = ',' .. good_types .. ',' local needle = ',' .. param_type(v) .. ',' if (string.find(haystack, needle) == nil) then - good_types = string.gsub(good_types, ',', ', ') box.error(box.error.ILLEGAL_PARAMS, "options parameter '" .. k .. "' should be one of types: " .. template[k]) @@ -582,9 +580,9 @@ end -- local function format_field_resolve(format, path, what) assert(type(path) == 'number' or type(path) == 'string') - local idx = nil + local idx local relative_path = nil - local field_name = nil + local field_name -- Path doesn't require resolve. if type(path) == 'number' then idx = path @@ -844,7 +842,7 @@ local function space_sequence_alter_prepare(format, parts, options, if id == nil then box.error(box.error.NO_SUCH_SEQUENCE, new_sequence.id) end - local tuple = _space_sequence.index.sequence:select(id)[1] + tuple = _space_sequence.index.sequence:select(id)[1] if tuple ~= nil and tuple.is_generated then box.error(box.error.ALTER_SPACE, space_name, "can not attach generated sequence") @@ -1144,7 +1142,7 @@ box.schema.index.alter = function(space_id, index_id, options) local can_update_field = {id = true, name = true, type = true } local can_update = true local cant_update_fields = '' - for k,v in pairs(options) do + for k, _ in pairs(options) do if not can_update_field[k] then can_update = false cant_update_fields = cant_update_fields .. ' ' .. k @@ -1188,7 +1186,7 @@ box.schema.index.alter = function(space_id, index_id, options) if options.type == nil then options.type = tuple.type end - for k, t in pairs(index_options) do + for k, _ in pairs(index_options) do if options[k] ~= nil then index_opts[k] = options[k] end @@ -1281,7 +1279,6 @@ end -- global struct port instance to use by select()/get() local port_tuple = ffi.new('struct port_tuple') -local port_tuple_entry_t = ffi.typeof('struct port_tuple_entry') -- Helper function to check space:method() usage local function check_space_arg(space, method) @@ -1499,7 +1496,8 @@ end base_index_mt.get_ffi = function(index, key) check_index_arg(index, 'get') - local key, key_end = tuple_encode(key) + local key_end + key, key_end = tuple_encode(key) if builtin.box_index_get(index.space_id, index.id, key, key_end, ptuple) ~= 0 then return box.error() -- error @@ -1532,7 +1530,8 @@ end base_index_mt.select_ffi = function(index, key, opts) check_index_arg(index, 'select') - local key, key_end = tuple_encode(key) + local key_end + key, key_end = tuple_encode(key) local iterator, offset, limit = check_select_opts(opts, key + 1 >= key_end) local port = ffi.cast('struct port *', port_tuple) @@ -1554,7 +1553,7 @@ end base_index_mt.select_luac = function(index, key, opts) check_index_arg(index, 'select') - local key = keify(key) + key = keify(key) local iterator, offset, limit = check_select_opts(opts, #key == 0) return internal.select(index.space_id, index.id, iterator, offset, limit, key) @@ -2489,20 +2488,20 @@ local function drop(uid, opts) -- recursive delete of user data local _vpriv = box.space[box.schema.VPRIV_ID] local spaces = box.space[box.schema.VSPACE_ID].index.owner:select{uid} - for k, tuple in pairs(spaces) do + for _, tuple in pairs(spaces) do box.space[tuple.id]:drop() end local funcs = box.space[box.schema.VFUNC_ID].index.owner:select{uid} - for k, tuple in pairs(funcs) do + for _, tuple in pairs(funcs) do box.schema.func.drop(tuple.id) end -- if this is a role, revoke this role from whoever it was granted to local grants = _vpriv.index.object:select{'role', uid} - for k, tuple in pairs(grants) do + for _, tuple in pairs(grants) do revoke(tuple.grantee, tuple.grantee, uid) end local sequences = box.space[box.schema.VSEQUENCE_ID].index.owner:select{uid} - for k, tuple in pairs(sequences) do + for _, tuple in pairs(sequences) do box.schema.sequence.drop(tuple.id) end -- xxx: hack, we have to revoke session and usage privileges @@ -2514,7 +2513,7 @@ local function drop(uid, opts) end local privs = _vpriv.index.primary:select{uid} - for k, tuple in pairs(privs) do + for _, tuple in pairs(privs) do -- we need an additional box.session.su() here, because of -- unnecessary check for privilege PRIV_REVOKE in priv_def_check() box.session.su("admin", revoke, uid, uid, tuple.privilege, @@ -2680,7 +2679,7 @@ box.once = function(key, func, ...) box.error(box.error.ILLEGAL_PARAMS, "Usage: box.once(key, func, ...)") end - local key = "once"..key + key = "once"..key if box.space._schema:get{key} ~= nil then return end diff --git a/src/box/lua/tuple.lua b/src/box/lua/tuple.lua index f97aa1579..1b958425f 100644 --- a/src/box/lua/tuple.lua +++ b/src/box/lua/tuple.lua @@ -1,7 +1,6 @@ -- tuple.lua (internal file) local ffi = require('ffi') -local yaml = require('yaml') local msgpackffi = require('msgpackffi') local fun = require('fun') local buffer = require('buffer') @@ -81,7 +80,6 @@ local tuple_encode = function(obj) encode_r(tmpbuf, obj, 1) elseif type(obj) == "table" then encode_array(tmpbuf, #obj) - local i for i = 1, #obj, 1 do encode_r(tmpbuf, obj[i], 1) end @@ -232,7 +230,7 @@ local function tuple_update(tuple, expr) error("Usage: tuple:update({ { op, field, arg}+ })") end local pexpr, pexpr_end = tuple_encode(expr) - local tuple = builtin.box_tuple_update(tuple, pexpr, pexpr_end) + tuple = builtin.box_tuple_update(tuple, pexpr, pexpr_end) if tuple == nil then return box.error() end @@ -245,7 +243,7 @@ local function tuple_upsert(tuple, expr) error("Usage: tuple:upsert({ { op, field, arg}+ })") end local pexpr, pexpr_end = tuple_encode(expr) - local tuple = builtin.box_tuple_upsert(tuple, pexpr, pexpr_end) + tuple = builtin.box_tuple_upsert(tuple, pexpr, pexpr_end) if tuple == nil then return box.error() end @@ -339,7 +337,7 @@ ffi.metatype(tuple_t, { ffi.metatype(tuple_iterator_t, { __call = tuple_iterator_next; - __tostring = function(it) return "" end; + __tostring = function() return "" end; }) -- Free methods, which are not needed anymore. diff --git a/src/box/lua/upgrade.lua b/src/box/lua/upgrade.lua index 075cc236e..208b53f15 100644 --- a/src/box/lua/upgrade.lua +++ b/src/box/lua/upgrade.lua @@ -52,10 +52,9 @@ end local function truncate(space) local pk = space.index[0] while pk:len() > 0 do - local state, t - for state, t in pk:pairs() do + for _, t in pk:pairs() do local key = {} - for _k2, parts in ipairs(pk.parts) do + for _, parts in ipairs(pk.parts) do table.insert(key, t[parts.fieldno]) end space:delete(key) @@ -173,7 +172,7 @@ local function initial_1_7_5() format[5] = {name='field_count', type='unsigned'} format[6] = {name='flags', type='map'} format[7] = {name='format', type='array'} - local def = _space:insert{_space.id, ADMIN, '_space', 'memtx', 0, MAP, format} + _space:insert{_space.id, ADMIN, '_space', 'memtx', 0, MAP, format} -- space name is unique log.info("create index primary on _space") _index:insert{_space.id, 0, 'primary', 'tree', { unique = true }, {{0, 'unsigned'}}} @@ -194,7 +193,7 @@ local function initial_1_7_5() format[4] = {name = 'type', type = 'string'} format[5] = {name = 'opts', type = 'map'} format[6] = {name = 'parts', type = 'array'} - def = _space:insert{_index.id, ADMIN, '_index', 'memtx', 0, MAP, format} + _space:insert{_index.id, ADMIN, '_index', 'memtx', 0, MAP, format} -- index name is unique within a space log.info("create index primary on _index") _index:insert{_index.id, 0, 'primary', 'tree', {unique = true}, {{0, 'unsigned'}, {1, 'unsigned'}}} @@ -211,7 +210,7 @@ local function initial_1_7_5() format[2] = {name='owner', type='unsigned'} format[3] = {name='name', type='string'} format[4] = {name='setuid', type='unsigned'} - def = _space:insert{_func.id, ADMIN, '_func', 'memtx', 0, MAP, format} + _space:insert{_func.id, ADMIN, '_func', 'memtx', 0, MAP, format} -- function name and id are unique log.info("create index _func:primary") _index:insert{_func.id, 0, 'primary', 'tree', {unique = true}, {{0, 'unsigned'}}} @@ -231,7 +230,7 @@ local function initial_1_7_5() format[3] = {name='name', type='string'} format[4] = {name='type', type='string'} format[5] = {name='auth', type='map'} - def = _space:insert{_user.id, ADMIN, '_user', 'memtx', 0, MAP, format} + _space:insert{_user.id, ADMIN, '_user', 'memtx', 0, MAP, format} -- user name and id are unique log.info("create index _func:primary") _index:insert{_user.id, 0, 'primary', 'tree', {unique = true}, {{0, 'unsigned'}}} @@ -251,7 +250,7 @@ local function initial_1_7_5() format[3] = {name='object_type', type='string'} format[4] = {name='object_id', type='unsigned'} format[5] = {name='privilege', type='unsigned'} - def = _space:insert{_priv.id, ADMIN, '_priv', 'memtx', 0, MAP, format} + _space:insert{_priv.id, ADMIN, '_priv', 'memtx', 0, MAP, format} -- user id, object type and object id are unique log.info("create index primary on _priv") _index:insert{_priv.id, 0, 'primary', 'tree', {unique = true}, {{1, 'unsigned'}, {2, 'string'}, {3, 'unsigned'}}} @@ -580,7 +579,7 @@ local function upgrade_to_2_1_0() -- Now, abscent field means NULL, so we can safely set second -- field in format, marking it nullable. log.info("Add nullable value field to space _schema") - local format = {} + format = {} format[1] = {type='string', name='key'} format[2] = {type='any', name='value', is_nullable=true} box.space._schema:format(format) @@ -734,7 +733,7 @@ local function upgrade_collation_to_2_1_3() local id = 4 for _, collation in ipairs(coll_lst) do - for i, strength in ipairs(coll_strengths) do + for _, strength in ipairs(coll_strengths) do local coll_name = 'unicode_' .. collation.name .. "_" .. strength.s log.info("creating collation %s", coll_name) box.space._collation:replace{id, coll_name, ADMIN, "ICU", collation.loc_str, strength.opt } -- 2.23.0