lint: Use early exit/continue

This commit is contained in:
Kurtis Rader 2016-10-30 12:52:50 -07:00
parent 49ed20c8cb
commit ca5a4ec1d5

View file

@ -216,22 +216,24 @@ static bool wildcard_complete_internal(const wchar_t *str, const wchar_t *wc,
match_acceptable = match_type_shares_prefix(match.type); match_acceptable = match_type_shares_prefix(match.type);
} }
if (match_acceptable && out != NULL) { if (!match_acceptable || out == NULL) {
// Wildcard complete. return match_acceptable;
bool full_replacement = match_type_requires_full_replacement(match.type) || }
(flags & COMPLETE_REPLACES_TOKEN);
// If we are not replacing the token, be careful to only store the part of the string // Wildcard complete.
// after the wildcard. bool full_replacement =
match_type_requires_full_replacement(match.type) || (flags & COMPLETE_REPLACES_TOKEN);
// If we are not replacing the token, be careful to only store the part of the string after
// the wildcard.
assert(!full_replacement || wcslen(wc) <= wcslen(str)); assert(!full_replacement || wcslen(wc) <= wcslen(str));
wcstring out_completion = full_replacement ? params.orig : str + wcslen(wc); wcstring out_completion = full_replacement ? params.orig : str + wcslen(wc);
wcstring out_desc = resolve_description(&out_completion, params.desc, params.desc_func); wcstring out_desc = resolve_description(&out_completion, params.desc, params.desc_func);
// Note: out_completion may be empty if the completion really is empty, e.g. // Note: out_completion may be empty if the completion really is empty, e.g. tab-completing
// tab-completing 'foo' when a file 'foo' exists. // 'foo' when a file 'foo' exists.
complete_flags_t local_flags = flags | (full_replacement ? COMPLETE_REPLACES_TOKEN : 0); complete_flags_t local_flags = flags | (full_replacement ? COMPLETE_REPLACES_TOKEN : 0);
append_completion(out, out_completion, out_desc, local_flags, match); append_completion(out, out_completion, out_desc, local_flags, match);
}
return match_acceptable; return match_acceptable;
} else if (next_wc_char_pos > 0) { } else if (next_wc_char_pos > 0) {
// Here we have a non-wildcard prefix. Note that we don't do fuzzy matching for stuff before // Here we have a non-wildcard prefix. Note that we don't do fuzzy matching for stuff before
@ -289,7 +291,10 @@ static bool wildcard_complete_internal(const wchar_t *str, const wchar_t *wc,
// We don't even try with this one. // We don't even try with this one.
return false; return false;
} }
default: { assert(0 && "Unreachable code reached"); } default: {
DIE("Unreachable code reached");
break;
}
} }
assert(0 && "Unreachable code reached"); assert(0 && "Unreachable code reached");
@ -322,17 +327,19 @@ bool wildcard_match(const wcstring &str, const wcstring &wc, bool leading_dots_f
/// \param err The errno value after a failed stat call on the file. /// \param err The errno value after a failed stat call on the file.
static wcstring file_get_desc(const wcstring &filename, int lstat_res, const struct stat &lbuf, static wcstring file_get_desc(const wcstring &filename, int lstat_res, const struct stat &lbuf,
int stat_res, const struct stat &buf, int err) { int stat_res, const struct stat &buf, int err) {
if (!lstat_res) { if (lstat_res) {
return COMPLETE_FILE_DESC;
}
if (S_ISLNK(lbuf.st_mode)) { if (S_ISLNK(lbuf.st_mode)) {
if (!stat_res) { if (!stat_res) {
if (S_ISDIR(buf.st_mode)) { if (S_ISDIR(buf.st_mode)) {
return COMPLETE_DIRECTORY_SYMLINK_DESC; return COMPLETE_DIRECTORY_SYMLINK_DESC;
} }
if (buf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH) && waccess(filename, X_OK) == 0) { if (buf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH) && waccess(filename, X_OK) == 0) {
// Weird group permissions and other such issues make it non-trivial to // Weird group permissions and other such issues make it non-trivial to find out if
// find out if we can actually execute a file using the result from // we can actually execute a file using the result from stat. It is much safer to
// stat. It is much safer to use the access function, since it tells us // use the access function, since it tells us exactly what we want to know.
// exactly what we want to know.
return COMPLETE_EXEC_LINK_DESC; return COMPLETE_EXEC_LINK_DESC;
} }
@ -353,16 +360,12 @@ static wcstring file_get_desc(const wcstring &filename, int lstat_res, const str
return COMPLETE_SOCKET_DESC; return COMPLETE_SOCKET_DESC;
} else if (S_ISDIR(buf.st_mode)) { } else if (S_ISDIR(buf.st_mode)) {
return COMPLETE_DIRECTORY_DESC; return COMPLETE_DIRECTORY_DESC;
} else { } else if (buf.st_mode & (S_IXUSR | S_IXGRP | S_IXGRP) && waccess(filename, X_OK) == 0) {
if (buf.st_mode & (S_IXUSR | S_IXGRP | S_IXGRP) && waccess(filename, X_OK) == 0) { // Weird group permissions and other such issues make it non-trivial to find out if we can
// Weird group permissions and other such issues make it non-trivial to find out // actually execute a file using the result from stat. It is much safer to use the access
// if we can actually execute a file using the result from stat. It is much // function, since it tells us exactly what we want to know.
// safer to use the access function, since it tells us exactly what we want to
// know.
return COMPLETE_EXEC_DESC; return COMPLETE_EXEC_DESC;
} }
}
}
return COMPLETE_FILE_DESC; return COMPLETE_FILE_DESC;
} }