From 00303ed07ffd3687d01e5e3ac43904e8e48efcd0 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Thu, 20 Oct 2016 18:53:31 -0700 Subject: [PATCH] lint cleanup: parameter reassignment --- src/builtin.cpp | 2 +- src/builtin_commandline.cpp | 10 ++++------ src/builtin_set.cpp | 23 ++++++----------------- src/common.cpp | 2 +- src/common.h | 2 +- src/env.cpp | 5 +++-- src/env_universal_common.cpp | 2 +- src/fallback.cpp | 2 +- src/output.cpp | 9 +++------ src/parse_util.cpp | 9 +++------ src/parser.cpp | 4 +--- src/reader.cpp | 6 +++--- src/util.cpp | 4 ++-- src/wgetopt.cpp | 5 +++-- 14 files changed, 33 insertions(+), 52 deletions(-) diff --git a/src/builtin.cpp b/src/builtin.cpp index 06944591b..5b5fa6683 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -412,7 +412,7 @@ static int builtin_bind_erase(wchar_t **seq, int all, const wchar_t *mode, int u } int res = 0; - if (mode == NULL) mode = DEFAULT_BIND_MODE; + if (mode == NULL) mode = DEFAULT_BIND_MODE; //!OCLINT(parameter reassignment) while (*seq) { if (use_terminfo) { diff --git a/src/builtin_commandline.cpp b/src/builtin_commandline.cpp index fd44975dd..4afa04f7d 100644 --- a/src/builtin_commandline.cpp +++ b/src/builtin_commandline.cpp @@ -165,16 +165,14 @@ static void write_part(const wchar_t *begin, const wchar_t *end, int cut_at_curs } streams.out.append(out); - free(buff); } else { if (cut_at_cursor) { - end = begin + pos; + streams.out.append(begin, pos); + } else { + streams.out.append(begin, end - begin); } - - // debug( 0, L"woot2 %ls -> %ls", buff, esc ); - streams.out.append(begin, end - begin); - streams.out.append(L"\n"); + streams.out.push_back(L'\n'); } } diff --git a/src/builtin_set.cpp b/src/builtin_set.cpp index 9eeea3172..0427427ec 100644 --- a/src/builtin_set.cpp +++ b/src/builtin_set.cpp @@ -159,7 +159,6 @@ static int my_env_set(const wchar_t *key, const wcstring_list_t &val, int scope, static int parse_index(std::vector &indexes, const wchar_t *src, const wchar_t *name, size_t var_count, io_streams_t &streams) { size_t len; - int count = 0; const wchar_t *src_orig = src; @@ -167,9 +166,7 @@ static int parse_index(std::vector &indexes, const wchar_t *src, const wch return 0; } - while (*src != L'\0' && (iswalnum(*src) || *src == L'_')) { - src++; - } + while (*src != L'\0' && (iswalnum(*src) || *src == L'_')) src++; if (*src != L'[') { streams.err.append_format(_(BUILTIN_SET_ARG_COUNT), L"set"); @@ -177,7 +174,6 @@ static int parse_index(std::vector &indexes, const wchar_t *src, const wch } len = src - src_orig; - if ((wcsncmp(src_orig, name, len) != 0) || (wcslen(name) != (len))) { streams.err.append_format( _(L"%ls: Multiple variable names specified in single call (%ls and %.*ls)\n"), L"set", @@ -186,37 +182,29 @@ static int parse_index(std::vector &indexes, const wchar_t *src, const wch } src++; - - while (iswspace(*src)) { - src++; - } + while (iswspace(*src)) src++; while (*src != L']') { wchar_t *end; - long l_ind; errno = 0; - l_ind = wcstol(src, &end, 10); - if (end == src || errno) { streams.err.append_format(_(L"%ls: Invalid index starting at '%ls'\n"), L"set", src); return 0; } - if (l_ind < 0) { - l_ind = var_count + l_ind + 1; - } + if (l_ind < 0) l_ind = var_count + l_ind + 1; - src = end; + src = end; //!OCLINT(parameter reassignment) if (*src == L'.' && *(src + 1) == L'.') { src += 2; long l_ind2 = wcstol(src, &end, 10); if (end == src || errno) { return 1; } - src = end; + src = end; //!OCLINT(parameter reassignment) if (l_ind2 < 0) { l_ind2 = var_count + l_ind2 + 1; @@ -231,6 +219,7 @@ static int parse_index(std::vector &indexes, const wchar_t *src, const wch indexes.push_back(l_ind); count++; } + while (iswspace(*src)) src++; } diff --git a/src/common.cpp b/src/common.cpp index 22235cdad..5c9d40e0a 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -107,6 +107,7 @@ demangled_backtrace(int max_frames, int skip_levels) { void __attribute__((noinline)) show_stackframe(const wchar_t msg_level, int frame_count, int skip_levels) { ASSERT_IS_NOT_FORKED_CHILD(); + if (frame_count < 1) return; // TODO: Decide if this is still needed. I'm commenting it out because it caused me some grief // while trying to debug a test failure. And the tests run just fine without spurious failures @@ -115,7 +116,6 @@ show_stackframe(const wchar_t msg_level, int frame_count, int skip_levels) { // Hack to avoid showing backtraces in the tester. // if (program_name && !wcscmp(program_name, L"(ignore)")) return; - if (frame_count < 1) frame_count = 999; debug_shared(msg_level, L"Backtrace:"); std::vector bt = demangled_backtrace(frame_count, skip_levels + 2); for (int i = 0; (size_t)i < bt.size(); i++) { diff --git a/src/common.h b/src/common.h index 2467b2429..3afae7a6c 100644 --- a/src/common.h +++ b/src/common.h @@ -258,7 +258,7 @@ extern bool has_working_tty_timestamps; #define contains(str, ...) contains_internal(str, 0, __VA_ARGS__, NULL) /// Print a stack trace to stderr. -void show_stackframe(const wchar_t msg_level, int frame_count = -1, int skip_levels = 0); +void show_stackframe(const wchar_t msg_level, int frame_count = 100, int skip_levels = 0); /// Read a line from the stream f into the string. Returns the number of bytes read or -1 on /// failure. diff --git a/src/env.cpp b/src/env.cpp index 79a1e0f0c..b708298b8 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -521,7 +521,7 @@ int env_set(const wcstring &key, const wchar_t *val, env_mode_flags_t var_mode) // Zero element arrays are internaly not coded as null but as this placeholder string. if (!val) { - val = ENV_NULL; + val = ENV_NULL; //!OCLINT(parameter reassignment) } if (var_mode & ENV_UNIVERSAL) { @@ -569,7 +569,8 @@ int env_set(const wcstring &key, const wchar_t *val, env_mode_flags_t var_mode) if ((var_mode & (ENV_EXPORT | ENV_UNEXPORT)) == 0) { // use existing entry's exportv - var_mode = preexisting_entry_exportv ? ENV_EXPORT : 0; + var_mode = + preexisting_entry_exportv ? ENV_EXPORT : 0; //!OCLINT(parameter reassignment) } } else { if (!get_proc_had_barrier()) { diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index a77eb6a6e..2bb11c521 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -1460,7 +1460,7 @@ universal_notifier_t &universal_notifier_t::default_notifier() { universal_notifier_t *universal_notifier_t::new_notifier_for_strategy( universal_notifier_t::notifier_strategy_t strat, const wchar_t *test_path) { if (strat == strategy_default) { - strat = resolve_default_strategy(); + strat = resolve_default_strategy(); //!OCLINT(parameter reassignment) } switch (strat) { case strategy_shmem_polling: { diff --git a/src/fallback.cpp b/src/fallback.cpp index 0c386fa1e..a9e9140f3 100644 --- a/src/fallback.cpp +++ b/src/fallback.cpp @@ -353,7 +353,7 @@ static int bisearch(wchar_t ucs, const struct interval *table, int max) { if (ucs > table[mid].last) min = mid + 1; else if (ucs < table[mid].first) - max = mid - 1; + max = mid - 1; //!OCLINT(parameter reassignment) else return 1; } diff --git a/src/output.cpp b/src/output.cpp index 2533a0dd1..9243730d4 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -312,13 +312,10 @@ int writech(wint_t ch) { if (ch >= ENCODE_DIRECT_BASE && ch < ENCODE_DIRECT_BASE + 256) { buff[0] = ch - ENCODE_DIRECT_BASE; len = 1; - } else if (MB_CUR_MAX == 1) // single-byte locale (C/POSIX/ISO-8859) - { + } else if (MB_CUR_MAX == 1) { + // single-byte locale (C/POSIX/ISO-8859) // If `wc` contains a wide character we emit a question-mark. - if (ch & ~0xFF) { - ch = '?'; - } - buff[0] = ch; + buff[0] = ch & ~0xFF ? '?' : ch; len = 1; } else { mbstate_t state = {}; diff --git a/src/parse_util.cpp b/src/parse_util.cpp index 509c6a612..1cd4f895c 100644 --- a/src/parse_util.cpp +++ b/src/parse_util.cpp @@ -88,13 +88,11 @@ size_t parse_util_get_offset(const wcstring &str, int line, long line_offset) { size_t off2 = parse_util_get_offset_from_line(buff, line + 1); if (off == (size_t)-1) return (size_t)-1; - if (off2 == (size_t)-1) off2 = wcslen(buff) + 1; - - if (line_offset < 0) line_offset = 0; + if (line_offset < 0) line_offset = 0; //!OCLINT(parameter reassignment) if ((size_t)line_offset >= off2 - off - 1) { - line_offset = off2 - off - 1; + line_offset = off2 - off - 1; //!OCLINT(parameter reassignment) } return off + line_offset; @@ -763,10 +761,9 @@ static int parser_is_pipe_forbidden(const wcstring &word) { bool parse_util_argument_is_help(const wchar_t *s, int min_match) { CHECK(s, 0); - size_t len = wcslen(s); - min_match = maxi(min_match, 3); + min_match = maxi(min_match, 3); //!OCLINT(parameter reassignment) return wcscmp(L"-h", s) == 0 || (len >= (size_t)min_match && (wcsncmp(L"--help", s, len) == 0)); } diff --git a/src/parser.cpp b/src/parser.cpp index 17ada81c0..d81ccac77 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -713,9 +713,7 @@ bool parser_t::detect_errors_in_argument_list(const wcstring &arg_list_src, wcst parse_error_list_t errors; // Use empty string for the prefix if it's NULL. - if (prefix == NULL) { - prefix = L""; - } + if (!prefix) prefix = L""; //!OCLINT(parameter reassignment) // Parse the string as an argument list. parse_node_tree_t tree; diff --git a/src/reader.cpp b/src/reader.cpp index 88f13cee2..947cd09bc 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -135,8 +135,8 @@ static void set_command_line_and_position(editable_line_t *el, const wcstring &n void editable_line_t::insert_string(const wcstring &str, size_t start, size_t len) { // Clamp the range to something valid. size_t string_length = str.size(); - start = mini(start, string_length); - len = mini(len, string_length - start); + start = mini(start, string_length); //!OCLINT(parameter reassignment) + len = mini(len, string_length - start); //!OCLINT(parameter reassignment) this->text.insert(this->position, str, start, len); this->position += len; } @@ -1898,7 +1898,7 @@ static void reader_set_buffer_maintaining_pager(const wcstring &b, size_t pos) { data->command_line_changed(&data->command_line); // Don't set a position past the command line length. - if (pos > command_line_len) pos = command_line_len; + if (pos > command_line_len) pos = command_line_len; //!OCLINT(parameter reassignment) update_buff_pos(&data->command_line, pos); diff --git a/src/util.cpp b/src/util.cpp index f13334807..b8f868094 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -47,8 +47,8 @@ int wcsfilecmp(const wchar_t *a, const wchar_t *b) { secondary_diff = (aend - a) - (bend - b); - a = aend - 1; - b = bend - 1; + a = aend - 1; //!OCLINT(parameter reassignment) + b = bend - 1; //!OCLINT(parameter reassignment) } else { int diff = towlower(*a) - towlower(*b); if (diff != 0) return diff > 0 ? 2 : -2; diff --git a/src/wgetopt.cpp b/src/wgetopt.cpp index f31b623b0..091bceeab 100644 --- a/src/wgetopt.cpp +++ b/src/wgetopt.cpp @@ -161,8 +161,9 @@ const wchar_t *wgetopter_t::_wgetopt_initialize(const wchar_t *optstring) { } else if (optstring[0] == '+') { ordering = REQUIRE_ORDER; ++optstring; - } else + } else { ordering = PERMUTE; + } return optstring; } @@ -211,7 +212,7 @@ int wgetopter_t::_wgetopt_internal(int argc, wchar_t **argv, const wchar_t *opts const struct woption *longopts, int *longind, int long_only) { woptarg = NULL; - if (woptind == 0) optstring = _wgetopt_initialize(optstring); + if (woptind == 0) optstring = _wgetopt_initialize(optstring); //!OCLINT(parameter reassignment) if (nextchar == NULL || *nextchar == '\0') { // Advance to the next ARGV-element.