diff --git a/src/builtin_test.cpp b/src/builtin_test.cpp index a9b640e9f..b6851ebf5 100644 --- a/src/builtin_test.cpp +++ b/src/builtin_test.cpp @@ -559,7 +559,7 @@ unique_ptr test_parser::parse_args(const wcstring_list_t &args, wcst int narg = 0; int len_to_err = 0; wcstring commandline; - for (auto arg : args) { + for (const wcstring &arg : args) { if (narg > 0) { commandline.append(L" "); } diff --git a/src/env.cpp b/src/env.cpp index 9ae4bc2d1..8d9ad1a12 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -82,7 +82,7 @@ struct electric_var_t { }; // Keep sorted alphabetically -static const std::vector electric_variables { +static const std::vector electric_variables{ {L"FISH_VERSION", electric_var_t::freadonly}, {L"PWD", electric_var_t::freadonly | electric_var_t::fcomputed | electric_var_t::fexports}, {L"SHLVL", electric_var_t::freadonly | electric_var_t::fexports}, @@ -101,11 +101,11 @@ static const std::vector electric_variables { const electric_var_t *electric_var_t::for_name(const wcstring &name) { static auto first = electric_variables.begin(); static auto last = electric_variables.end(); - electric_var_t search { name.c_str(), 0 }; + electric_var_t search{name.c_str(), 0}; auto binsearch = std::lower_bound(first, last, search, - [&](const electric_var_t &v1, const electric_var_t &v2) { - return wcscmp(v1.name, v2.name) < 0; - }); + [&](const electric_var_t &v1, const electric_var_t &v2) { + return wcscmp(v1.name, v2.name) < 0; + }); if (binsearch != last && wcscmp(name.c_str(), binsearch->name) == 0) { return &*binsearch; } @@ -637,7 +637,7 @@ std::shared_ptr> env_scoped_impl_t::create_e // which we depend on here. // Note: Using std::move around make_pair prevents the compiler from implementing // copy elision. - vals.insert(std::make_pair(std::move(key), std::move(*var))); + vals.insert(std::make_pair(key, std::move(*var))); } } diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index 295ad5884..bd137c9bd 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -442,7 +442,9 @@ std::string env_universal_t::serialize_with_vars(const var_table_t &vars) { contents.append("# VERSION: " UVARS_VERSION_3_0 "\n"); // Preserve legacy behavior by sorting the values first - typedef std::pair, std::reference_wrapper> env_pair_t; + typedef std::pair, + std::reference_wrapper> + env_pair_t; std::vector cloned(vars.begin(), vars.end()); std::sort(cloned.begin(), cloned.end(), [](const env_pair_t &p1, const env_pair_t &p2) { return p1.first.get() < p2.first.get(); diff --git a/src/expand.cpp b/src/expand.cpp index 2ef7a34f7..e8f4b694b 100644 --- a/src/expand.cpp +++ b/src/expand.cpp @@ -645,7 +645,7 @@ static expand_result_t expand_cmdsubst(wcstring input, const operation_context_t if (*tail_begin == L'[') { std::vector slice_idx; const wchar_t *const slice_begin = tail_begin; - wchar_t *slice_end; + wchar_t *slice_end = nullptr; size_t bad_pos; bad_pos = parse_slice(slice_begin, &slice_end, slice_idx, sub_res.size()); diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 9c45ad749..e0f52fe47 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -349,13 +349,13 @@ static void test_unescape_sane() { {L"'\\143'", L"\\143"}, {L"\\n", L"\n"} // \n normally becomes newline }; wcstring output; - for (size_t i = 0; i < sizeof tests / sizeof *tests; i++) { - bool ret = unescape_string(tests[i].input, &output, UNESCAPE_DEFAULT); + for (const auto &test : tests) { + bool ret = unescape_string(test.input, &output, UNESCAPE_DEFAULT); if (!ret) { - err(L"Failed to unescape '%ls'\n", tests[i].input); - } else if (output != tests[i].expected) { - err(L"In unescaping '%ls', expected '%ls' but got '%ls'\n", tests[i].input, - tests[i].expected, output.c_str()); + err(L"Failed to unescape '%ls'\n", test.input); + } else if (output != test.expected) { + err(L"In unescaping '%ls', expected '%ls' but got '%ls'\n", test.input, test.expected, + output.c_str()); } } @@ -451,11 +451,10 @@ static void test_format() { const char *expected; } tests[] = {{0, "empty"}, {1, "1B"}, {2, "2B"}, {1024, "1kB"}, {1870, "1.8kB"}, {4322911, "4.1MB"}}; - size_t i; - for (i = 0; i < sizeof tests / sizeof *tests; i++) { + for (const auto &test : tests) { char buff[128]; - format_size_safe(buff, tests[i].val); - do_test(!std::strcmp(buff, tests[i].expected)); + format_size_safe(buff, test.val); + do_test(!std::strcmp(buff, test.expected)); } for (int j = -129; j <= 129; j++) { @@ -1876,12 +1875,11 @@ static bool expand_test(const wchar_t *in, expand_flags_t flags, ...) { } msg += L"], found ["; first = true; - for (completion_list_t::const_iterator it = output.begin(), end = output.end(); - it != end; ++it) { + for (const auto &completion : output) { if (!first) msg += L", "; first = false; msg += '"'; - msg += it->completion; + msg += completion.completion; msg += '"'; } msg += L"]"; @@ -2372,8 +2370,7 @@ static void test_1_word_motion(word_motion_t motion, move_word_style_t style, std::set stops; // Carets represent stops and should be cut out of the command. - for (size_t i = 0; i < test.size(); i++) { - wchar_t wc = test.at(i); + for (wchar_t wc : test) { if (wc == L'^') { stops.insert(command.size()); } else { @@ -3330,8 +3327,8 @@ static void test_input() { } // Push the desired binding to the queue. - for (size_t idx = 0; idx < desired_binding.size(); idx++) { - input.queue_ch(desired_binding.at(idx)); + for (wchar_t c : desired_binding) { + input.queue_ch(c); } // Now test. @@ -3963,9 +3960,9 @@ void history_tests_t::test_history_races() { } // Wait for all children. - for (size_t i = 0; i < RACE_COUNT; i++) { + for (pid_t child : children) { int stat; - waitpid(children[i], &stat, WUNTRACED); + waitpid(child, &stat, WUNTRACED); } // Compute the expected lines. @@ -4044,8 +4041,8 @@ void history_tests_t::test_history_merge() { const wcstring alt_texts[count] = {L"History Alt 1", L"History Alt 2", L"History Alt 3"}; // Make sure history is clear. - for (size_t i = 0; i < count; i++) { - hists[i]->clear(); + for (auto &hist : hists) { + hist->clear(); } // Make sure we don't add an item in the same second as we created the history. @@ -4057,8 +4054,8 @@ void history_tests_t::test_history_merge() { } // Save them. - for (size_t i = 0; i < count; i++) { - hists[i]->save(); + for (auto &hist : hists) { + hist->save(); } // Make sure each history contains what it ought to, but they have not leaked into each other. @@ -4074,21 +4071,21 @@ void history_tests_t::test_history_merge() { // is newer, since we only pick up items whose timestamp is before the birth stamp. time_barrier(); std::unique_ptr everything = make_unique(name); - for (size_t i = 0; i < count; i++) { - do_test(history_contains(everything, texts[i])); + for (const auto &text : texts) { + do_test(history_contains(everything, text)); } // Tell all histories to merge. Now everybody should have everything. - for (size_t i = 0; i < count; i++) { - hists[i]->incorporate_external_changes(); + for (auto &hist : hists) { + hist->incorporate_external_changes(); } // Everyone should also have items in the same order (#2312) wcstring_list_t hist_vals1; hists[0]->get_history(hist_vals1); - for (size_t i = 0; i < count; i++) { + for (const auto &hist : hists) { wcstring_list_t hist_vals2; - hists[i]->get_history(hist_vals2); + hist->get_history(hist_vals2); do_test(hist_vals1 == hist_vals2); } @@ -4304,15 +4301,13 @@ static void test_new_parser_correctness() { {L"true || \n\n false", true}, }; - for (size_t i = 0; i < sizeof parser_tests / sizeof *parser_tests; i++) { - const parser_test_t *test = &parser_tests[i]; - + for (const auto &test : parser_tests) { parse_node_tree_t parse_tree; - bool success = parse_tree_from_string(test->src, parse_flag_none, &parse_tree, NULL); - if (success && !test->ok) { - err(L"\"%ls\" should NOT have parsed, but did", test->src); - } else if (!success && test->ok) { - err(L"\"%ls\" should have parsed, but failed", test->src); + bool success = parse_tree_from_string(test.src, parse_flag_none, &parse_tree, NULL); + if (success && !test.ok) { + err(L"\"%ls\" should NOT have parsed, but did", test.src); + } else if (!success && test.ok) { + err(L"\"%ls\" should have parsed, but failed", test.src); } } say(L"Parse tests complete"); @@ -4452,21 +4447,20 @@ static void test_new_parser_ll2() { {L"function", L"function", L"", parse_statement_decoration_none}, {L"function --help", L"function", L"--help", parse_statement_decoration_none}}; - for (size_t i = 0; i < sizeof tests / sizeof *tests; i++) { + for (const auto &test : tests) { wcstring cmd, args; enum parse_statement_decoration_t deco = parse_statement_decoration_none; - bool success = test_1_parse_ll2(tests[i].src, &cmd, &args, &deco); - if (!success) - err(L"Parse of '%ls' failed on line %ld", tests[i].cmd.c_str(), (long)__LINE__); - if (cmd != tests[i].cmd) + bool success = test_1_parse_ll2(test.src, &cmd, &args, &deco); + if (!success) err(L"Parse of '%ls' failed on line %ld", test.cmd.c_str(), (long)__LINE__); + if (cmd != test.cmd) err(L"When parsing '%ls', expected command '%ls' but got '%ls' on line %ld", - tests[i].src.c_str(), tests[i].cmd.c_str(), cmd.c_str(), (long)__LINE__); - if (args != tests[i].args) + test.src.c_str(), test.cmd.c_str(), cmd.c_str(), (long)__LINE__); + if (args != test.args) err(L"When parsing '%ls', expected args '%ls' but got '%ls' on line %ld", - tests[i].src.c_str(), tests[i].args.c_str(), args.c_str(), (long)__LINE__); - if (deco != tests[i].deco) + test.src.c_str(), test.args.c_str(), args.c_str(), (long)__LINE__); + if (deco != test.deco) err(L"When parsing '%ls', expected decoration %d but got %d on line %ld", - tests[i].src.c_str(), (int)tests[i].deco, (int)deco, (long)__LINE__); + test.src.c_str(), (int)test.deco, (int)deco, (long)__LINE__); } check_function_help(L"function -h"); @@ -4516,9 +4510,9 @@ static void test_new_parser_errors() { {L"if true ; case ; end", parse_error_unbalancing_case}, }; - for (size_t i = 0; i < sizeof tests / sizeof *tests; i++) { - const wcstring src = tests[i].src; - parse_error_code_t expected_code = tests[i].code; + for (const auto &test : tests) { + const wcstring src = test.src; + parse_error_code_t expected_code = test.code; parse_error_list_t errors; parse_node_tree_t parse_tree; @@ -4534,8 +4528,8 @@ static void test_new_parser_errors() { err(L"Source '%ls' was expected to produce error code %lu, but instead produced error " L"code %lu", src.c_str(), expected_code, (unsigned long)errors.at(0).code); - for (size_t i = 0; i < errors.size(); i++) { - err(L"\t\t%ls", errors.at(i).describe(src, true).c_str()); + for (const auto &error : errors) { + err(L"\t\t%ls", error.describe(src, true).c_str()); } } } @@ -4597,8 +4591,7 @@ static bool string_matches_format(const wcstring &string, const wchar_t *format) bool result = true; wcstring_list_t components = separate_by_format_specifiers(format); size_t idx = 0; - for (size_t i = 0; i < components.size(); i++) { - const wcstring &component = components.at(i); + for (const auto &component : components) { size_t where = string.find(component, idx); if (where == wcstring::npos) { result = false; @@ -4632,13 +4625,12 @@ static void test_error_messages() { {L"echo \"foo$(foo)bar\"", ERROR_BAD_VAR_SUBCOMMAND1}}; parse_error_list_t errors; - for (size_t i = 0; i < sizeof error_tests / sizeof *error_tests; i++) { - const struct error_test_t *test = &error_tests[i]; + for (const auto &test : error_tests) { errors.clear(); - parse_util_detect_errors(test->src, &errors, false /* allow_incomplete */); + parse_util_detect_errors(test.src, &errors, false /* allow_incomplete */); do_test(!errors.empty()); if (!errors.empty()) { - do_test1(string_matches_format(errors.at(0).text, test->error_text_format), test->src); + do_test1(string_matches_format(errors.at(0).text, test.error_text_format), test.src); } } } diff --git a/src/parse_grammar_elements.inc b/src/parse_grammar_elements.inc index 343af206e..621792b7a 100644 --- a/src/parse_grammar_elements.inc +++ b/src/parse_grammar_elements.inc @@ -35,4 +35,3 @@ ELEM(optional_time) ELEM(end_command) ELEM(freestanding_argument_list) #undef ELEM - diff --git a/src/postfork.h b/src/postfork.h index b876ed6de..0b41976ab 100644 --- a/src/postfork.h +++ b/src/postfork.h @@ -20,12 +20,12 @@ class dup2_list_t; class job_t; class process_t; -/// Tell the proc \p pid to join process group \p pgrp. +/// Tell the proc \p pid to join process group \p pgroup. /// If \p is_child is true, we are the child process; otherwise we are fish. /// Called by both parent and child; this is an unavoidable race inherent to Unix. /// If is_parent is set, then we are the parent process and should swallow EACCESS. /// \return 0 on success, an errno error code on failure. -int execute_setpgid(pid_t pid, pid_t pgrp, bool is_parent); +int execute_setpgid(pid_t pid, pid_t pgroup, bool is_parent); /// Report the error code \p err for a failed setpgid call. /// Note not all errors should be reported; in particular EACCESS is expected and benign in the diff --git a/src/reader.cpp b/src/reader.cpp index 43a1e6246..ef2763731 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -1683,7 +1683,6 @@ bool reader_data_t::handle_completions(const completion_list_t &comp, size_t tok // No suitable completions found, flash screen and return. flash(); done = true; - success = false; } else if (size == 1) { // Exactly one suitable completion found - insert it. const completion_t &c = comp.at(0); @@ -1783,7 +1782,6 @@ bool reader_data_t::handle_completions(const completion_list_t &comp, size_t tok completion_insert(common_prefix.c_str(), token_end, flags); cycle_command_line = command_line.text(); cycle_cursor_pos = command_line.position(); - success = true; } } @@ -3434,7 +3432,7 @@ void reader_data_t::handle_readline_command(readline_cmd_t c, readline_loop_stat editable_line_t *el = active_edit_line(); bool success = false; jump_direction_t original_dir, dir; - original_dir = dir = last_jump_direction; + original_dir = last_jump_direction; if (last_jump_direction == jump_direction_t::forward) { dir = jump_direction_t::backward; diff --git a/src/termsize.cpp b/src/termsize.cpp index ee5d715bc..905d922ff 100644 --- a/src/termsize.cpp +++ b/src/termsize.cpp @@ -24,7 +24,7 @@ static maybe_t read_termsize_from_tty() { // static termsize_container_t &termsize_container_t::shared() { // Heap-allocated to avoid runtime dtor registration. - static termsize_container_t *res = new termsize_container_t(read_termsize_from_tty); + static auto *res = new termsize_container_t(read_termsize_from_tty); return *res; } diff --git a/src/wildcard.cpp b/src/wildcard.cpp index 9d1d15388..7a76ccd52 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -103,7 +103,7 @@ static enum fuzzy_match_type_t wildcard_match_internal(const wcstring &str, cons // Hackish fix for issue #270. Prevent wildcards from matching . or .., but we must still allow // literal matches. if (leading_dots_fail_to_match && str[0] == L'.' && - (str[1] == L'\0' || (str[1] == L'.' && str[2] == L'\0'))) { + (str[1] == L'\0' || (str[1] == L'.' && str[2] == L'\0'))) { // The string is '.' or '..' so the only possible match is an exact match. return str == wc ? fuzzy_match_exact : fuzzy_match_none; } @@ -203,20 +203,19 @@ static bool has_prefix_match(const completion_list_t *comps, size_t first) { /// /// We ignore ANY_STRING_RECURSIVE here. The consequence is that you cannot tab complete ** /// wildcards. This is historic behavior. -static bool wildcard_complete_internal(const wchar_t * const str, size_t str_len, - const wchar_t * const wc, size_t wc_len, +static bool wildcard_complete_internal(const wchar_t *const str, size_t str_len, + const wchar_t *const wc, size_t wc_len, const wc_complete_pack_t ¶ms, complete_flags_t flags, completion_list_t *out, bool is_first_call); -__attribute__((unused)) -static bool wildcard_complete_internal(const wchar_t * const str, const wchar_t * const wc, - const wc_complete_pack_t ¶ms, complete_flags_t flags, - completion_list_t *out, bool is_first_call = false) { - return wildcard_complete_internal( - str, std::wcslen(str), wc, std::wcslen(wc), params, flags, out, is_first_call); +__attribute__((unused)) static bool wildcard_complete_internal( + const wchar_t *const str, const wchar_t *const wc, const wc_complete_pack_t ¶ms, + complete_flags_t flags, completion_list_t *out, bool is_first_call = false) { + return wildcard_complete_internal(str, std::wcslen(str), wc, std::wcslen(wc), params, flags, + out, is_first_call); } -static bool wildcard_complete_internal(const wchar_t * const str, size_t str_len, - const wchar_t * const wc, size_t wc_len, +static bool wildcard_complete_internal(const wchar_t *const str, size_t str_len, + const wchar_t *const wc, size_t wc_len, const wc_complete_pack_t ¶ms, complete_flags_t flags, completion_list_t *out, bool is_first_call = false) { assert(str != nullptr); @@ -300,7 +299,8 @@ static bool wildcard_complete_internal(const wchar_t * const str, size_t str_len if (str[0] == L'\0') { return false; } - return wildcard_complete_internal(str + 1, str_len - 1, wc + 1, wc_len - 1, params, flags, out); + return wildcard_complete_internal(str + 1, str_len - 1, wc + 1, wc_len - 1, params, + flags, out); } case ANY_STRING: { // Hackish. If this is the last character of the wildcard, then just complete with @@ -316,7 +316,8 @@ static bool wildcard_complete_internal(const wchar_t * const str, size_t str_len bool has_match = false; for (size_t i = 0; str[i] != L'\0'; i++) { const size_t before_count = out ? out->size() : 0; - if (wildcard_complete_internal(str + i, str_len - i, wc + 1, wc_len - 1, params, flags, out)) { + if (wildcard_complete_internal(str + i, str_len - i, wc + 1, wc_len - 1, params, + flags, out)) { // We found a match. has_match = true; @@ -349,12 +350,11 @@ bool wildcard_complete(const wcstring &str, const wchar_t *wc, assert(wc != nullptr); wc_complete_pack_t params(str, desc_func, expand_flags); return wildcard_complete_internal(str.c_str(), str.size(), wc, std::wcslen(wc), params, flags, - out, true /* first call */); + out, true /* first call */); } bool wildcard_match(const wcstring &str, const wcstring &wc, bool leading_dots_fail_to_match) { - enum fuzzy_match_type_t match = - wildcard_match_internal(str, wc, leading_dots_fail_to_match); + enum fuzzy_match_type_t match = wildcard_match_internal(str, wc, leading_dots_fail_to_match); return match != fuzzy_match_none; } @@ -413,7 +413,6 @@ static int fast_waccess(const struct stat &stat_buf, uint8_t mode) { return -1; } - /// Obtain a description string for the file specified by the filename. /// /// The returned value is a string constant and should not be free'd.