lint: Use early exit/continue

This commit is contained in:
Kurtis Rader 2016-10-30 15:05:41 -07:00
parent eab836864e
commit 6192e2453e

View file

@ -122,7 +122,10 @@ bool is_potential_path(const wcstring &potential_path_fragment, const wcstring_l
}
}
if (!has_magic && !clean_potential_path_fragment.empty()) {
if (has_magic || clean_potential_path_fragment.empty()) {
return result;
}
// Don't test the same path multiple times, which can happen if the path is absolute and the
// CDPATH contains multiple entries.
std::set<wcstring> checked_paths;
@ -133,8 +136,7 @@ bool is_potential_path(const wcstring &potential_path_fragment, const wcstring_l
for (size_t wd_idx = 0; wd_idx < directories.size() && !result; wd_idx++) {
const wcstring &wd = directories.at(wd_idx);
const wcstring abs_path =
path_apply_working_directory(clean_potential_path_fragment, wd);
const wcstring abs_path = path_apply_working_directory(clean_potential_path_fragment, wd);
// Skip this if it's empty or we've already checked it.
if (abs_path.empty() || checked_paths.count(abs_path)) continue;
@ -162,9 +164,9 @@ bool is_potential_path(const wcstring &potential_path_fragment, const wcstring_l
wcstring matched_file;
// We opened the dir_name; look for a string where the base name prefixes it
// Don't ask for the is_dir value unless we care, because it can cause extra
// filesystem access.
// We opened the dir_name; look for a string where the base name prefixes it Don't
// ask for the is_dir value unless we care, because it can cause extra filesystem
// access.
wcstring ent;
bool is_dir = false;
while (wreaddir_resolving(dir, dir_name, ent, require_dir ? &is_dir : NULL)) {
@ -176,19 +178,17 @@ bool is_potential_path(const wcstring &potential_path_fragment, const wcstring_l
if (string_prefixes_string(filename_fragment, ent) ||
(do_case_insensitive &&
string_prefixes_string_case_insensitive(filename_fragment, ent))) {
// We matched.
matched_file = ent;
matched_file = ent; // we matched
break;
}
}
closedir(dir);
// We succeeded if we found a match.
result = !matched_file.empty();
}
result = !matched_file.empty(); // we succeeded if we found a match
}
}
}
return result;
}
@ -354,10 +354,12 @@ bool autosuggest_validate_from_history(const history_item_t &item,
}
}
// If not handled specially, handle it here.
if (!handled) {
bool cmd_ok = false;
if (handled) {
return suggestionOK;
}
// Not handled specially so handle it here.
bool cmd_ok = false;
if (path_get_path(parsed_command, NULL)) {
cmd_ok = true;
} else if (builtin_exists(parsed_command) ||
@ -373,7 +375,6 @@ bool autosuggest_validate_from_history(const history_item_t &item,
suggestionOK = detector.paths_are_valid(paths);
}
}
}
return suggestionOK;
}
@ -1093,7 +1094,10 @@ const highlighter_t::color_array_t &highlighter_t::highlight() {
// Color the command.
const parse_node_t *cmd_node =
parse_tree.get_child(node, 0, parse_token_type_string);
if (cmd_node != NULL && cmd_node->has_source()) {
if (cmd_node == NULL || !cmd_node->has_source()) {
break; // not much as we can do without a node that has source text
}
bool is_valid_cmd = false;
if (!this->io_ok) {
// We cannot check if the command is invalid, so just assume it's valid.
@ -1106,13 +1110,11 @@ const highlighter_t::color_array_t &highlighter_t::highlight() {
bool expanded = expand_one(
cmd, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES | EXPAND_SKIP_JOBS);
if (expanded && !has_expand_reserved(cmd)) {
is_valid_cmd =
command_is_valid(cmd, decoration, working_directory, vars);
is_valid_cmd = command_is_valid(cmd, decoration, working_directory, vars);
}
}
this->color_node(*cmd_node,
is_valid_cmd ? highlight_spec_command : highlight_spec_error);
}
break;
}
case symbol_arguments_or_redirections_list:
@ -1141,7 +1143,10 @@ const highlighter_t::color_array_t &highlighter_t::highlight() {
}
}
if (this->io_ok && this->cursor_pos <= this->buff.size()) {
if (!this->io_ok || this->cursor_pos > this->buff.size()) {
return color_array;
}
// If the cursor is over an argument, and that argument is a valid path, underline it.
for (parse_node_tree_t::const_iterator iter = parse_tree.begin(); iter != parse_tree.end();
++iter) {
@ -1150,15 +1155,13 @@ const highlighter_t::color_array_t &highlighter_t::highlight() {
// Must be an argument with source.
if (node.type != symbol_argument || !node.has_source()) continue;
// See if this node contains the cursor. We check <= source_length so that, when
// backspacing (and the cursor is just beyond the last token), we may still underline
// it.
// See if this node contains the cursor. We check <= source_length so that, when backspacing
// (and the cursor is just beyond the last token), we may still underline it.
if (this->cursor_pos >= node.source_start &&
this->cursor_pos - node.source_start <= node.source_length &&
node_is_potential_path(buff, node, working_directory)) {
// It is, underline it.
for (size_t i = node.source_start; i < node.source_start + node.source_length;
i++) {
for (size_t i = node.source_start; i < node.source_start + node.source_length; i++) {
// Don't color highlight_spec_error because it looks dorky. For example,
// trying to cd into a non-directory would show an underline and also red.
if (highlight_get_primary(this->color_array.at(i)) != highlight_spec_error) {
@ -1167,7 +1170,6 @@ const highlighter_t::color_array_t &highlighter_t::highlight() {
}
}
}
}
return color_array;
}