From 3bd24ddb17cafadfc4fc83cb5511f5977686fe13 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Sun, 30 Oct 2016 16:16:15 -0700 Subject: [PATCH] lint: Use early exit/continue --- src/complete.cpp | 212 ++++++++++++++++++++++++----------------------- 1 file changed, 108 insertions(+), 104 deletions(-) diff --git a/src/complete.cpp b/src/complete.cpp index 8cf1a4041..2f750f412 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -956,71 +956,73 @@ bool completer_t::complete_param(const wcstring &scmd_orig, const wcstring &spop } } - if (use_common) { - for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end(); - ++oiter) { - const complete_entry_opt_t *o = &*oiter; - // If this entry is for the base command, check if any of the arguments match. - if (!this->condition_test(o->condition)) continue; - if (o->option.empty()) { - use_files = use_files && ((o->result_mode & NO_FILES) == 0); - complete_from_args(str, o->comp, o->localized_desc(), o->flags); - } + if (!use_common) { + continue; + } - if (wcslen(str) > 0 && use_switches) { - // Check if the short style option matches. - if (short_ok(str, o, options)) { - // It's a match. - const wcstring desc = o->localized_desc(); - append_completion(&this->completions, o->option, desc, 0); - } - - // Check if the long style option matches. - if (o->type == option_type_single_long || o->type == option_type_double_long) { - int match = 0, match_no_case = 0; - - wcstring whole_opt(o->expected_dash_count(), L'-'); - whole_opt.append(o->option); - - match = string_prefixes_string(str, whole_opt); - - if (!match) { - match_no_case = wcsncasecmp(str, whole_opt.c_str(), wcslen(str)) == 0; - } - - if (match || match_no_case) { - int has_arg = 0; // does this switch have any known arguments - int req_arg = 0; // does this switch _require_ an argument - - size_t offset = 0; - complete_flags_t flags = 0; - - if (match) { - offset = wcslen(str); - } else { - flags = COMPLETE_REPLACES_TOKEN; - } - - has_arg = !o->comp.empty(); - req_arg = (o->result_mode & NO_COMMON); - - if (o->type == option_type_double_long && (has_arg && !req_arg)) { - // Optional arguments to a switch can only be handled using the '=', - // so we add it as a completion. By default we avoid using '=' and - // instead rely on '--switch switch-arg', since it is more commonly - // supported by homebrew getopt-like functions. - wcstring completion = - format_string(L"%ls=", whole_opt.c_str() + offset); - append_completion(&this->completions, completion, C_(o->desc), - flags); - } - - append_completion(&this->completions, whole_opt.c_str() + offset, - C_(o->desc), flags); - } - } - } + for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end(); + ++oiter) { + const complete_entry_opt_t *o = &*oiter; + // If this entry is for the base command, check if any of the arguments match. + if (!this->condition_test(o->condition)) continue; + if (o->option.empty()) { + use_files = use_files && ((o->result_mode & NO_FILES) == 0); + complete_from_args(str, o->comp, o->localized_desc(), o->flags); } + + if (wcslen(str) == 0 || !use_switches) { + continue; + } + + // Check if the short style option matches. + if (short_ok(str, o, options)) { + // It's a match. + const wcstring desc = o->localized_desc(); + append_completion(&this->completions, o->option, desc, 0); + } + + // Check if the long style option matches. + if (o->type != option_type_single_long && o->type != option_type_double_long) { + continue; + } + int match = 0, match_no_case = 0; + + wcstring whole_opt(o->expected_dash_count(), L'-'); + whole_opt.append(o->option); + + match = string_prefixes_string(str, whole_opt); + if (!match) { + match_no_case = wcsncasecmp(str, whole_opt.c_str(), wcslen(str)) == 0; + } + + if (!match && !match_no_case) { + continue; + } + + int has_arg = 0; // does this switch have any known arguments + int req_arg = 0; // does this switch _require_ an argument + size_t offset = 0; + complete_flags_t flags = 0; + + if (match) { + offset = wcslen(str); + } else { + flags = COMPLETE_REPLACES_TOKEN; + } + + has_arg = !o->comp.empty(); + req_arg = (o->result_mode & NO_COMMON); + + if (o->type == option_type_double_long && (has_arg && !req_arg)) { + // Optional arguments to a switch can only be handled using the '=', so we add it as + // a completion. By default we avoid using '=' and instead rely on '--switch + // switch-arg', since it is more commonly supported by homebrew getopt-like + // functions. + wcstring completion = format_string(L"%ls=", whole_opt.c_str() + offset); + append_completion(&this->completions, completion, C_(o->desc), flags); + } + + append_completion(&this->completions, whole_opt.c_str() + offset, C_(o->desc), flags); } } @@ -1196,50 +1198,52 @@ bool completer_t::try_complete_user(const wcstring &str) { #else const wchar_t *cmd = str.c_str(); const wchar_t *first_char = cmd; - int res = 0; - double start_time = timef(); - if (*first_char == L'~' && !wcschr(first_char, L'/')) { - const wchar_t *user_name = first_char + 1; - const wchar_t *name_end = wcschr(user_name, L'~'); - if (name_end == 0) { - struct passwd *pw; - size_t name_len = wcslen(user_name); - - setpwent(); - - while ((pw = getpwent()) != 0) { - double current_time = timef(); - - if (current_time - start_time > 0.2) { - return 1; - } - - if (pw->pw_name) { - const wcstring pw_name_str = str2wcstring(pw->pw_name); - const wchar_t *pw_name = pw_name_str.c_str(); - if (wcsncmp(user_name, pw_name, name_len) == 0) { - wcstring desc = format_string(COMPLETE_USER_DESC, pw_name); - append_completion(&this->completions, &pw_name[name_len], desc, - COMPLETE_NO_SPACE); - - res = 1; - } else if (wcsncasecmp(user_name, pw_name, name_len) == 0) { - wcstring name = format_string(L"~%ls", pw_name); - wcstring desc = format_string(COMPLETE_USER_DESC, pw_name); - - append_completion(&this->completions, name, desc, COMPLETE_REPLACES_TOKEN | - COMPLETE_DONT_ESCAPE | - COMPLETE_NO_SPACE); - res = 1; - } - } - } - endpwent(); - } + if (*first_char != L'~' || wcschr(first_char, L'/')) { + return false; } - return res; + const wchar_t *user_name = first_char + 1; + const wchar_t *name_end = wcschr(user_name, L'~'); + if (name_end) { + return false; + } + + double start_time = timef(); + bool result = false; + struct passwd *pw; + size_t name_len = wcslen(user_name); + + setpwent(); + while ((pw = getpwent()) != 0) { + double current_time = timef(); + if (current_time - start_time > 0.2) { + return 1; + } + + if (!pw->pw_name) { + continue; + } + + const wcstring pw_name_str = str2wcstring(pw->pw_name); + const wchar_t *pw_name = pw_name_str.c_str(); + if (wcsncmp(user_name, pw_name, name_len) == 0) { + wcstring desc = format_string(COMPLETE_USER_DESC, pw_name); + append_completion(&this->completions, &pw_name[name_len], desc, COMPLETE_NO_SPACE); + + result = true; + } else if (wcsncasecmp(user_name, pw_name, name_len) == 0) { + wcstring name = format_string(L"~%ls", pw_name); + wcstring desc = format_string(COMPLETE_USER_DESC, pw_name); + + append_completion(&this->completions, name, desc, COMPLETE_REPLACES_TOKEN | + COMPLETE_DONT_ESCAPE | + COMPLETE_NO_SPACE); + result = true; + } + } + endpwent(); + return result; #endif }