From ca5a4ec1d54a9efcaed16a58c4ea0991a359946c Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Sun, 30 Oct 2016 12:52:50 -0700 Subject: [PATCH] lint: Use early exit/continue --- src/wildcard.cpp | 107 ++++++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 52 deletions(-) diff --git a/src/wildcard.cpp b/src/wildcard.cpp index ffd0e6273..43659995f 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -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); } - if (match_acceptable && out != NULL) { - // Wildcard complete. - 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)); - wcstring out_completion = full_replacement ? params.orig : str + wcslen(wc); - 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. - // tab-completing 'foo' when a file 'foo' exists. - complete_flags_t local_flags = flags | (full_replacement ? COMPLETE_REPLACES_TOKEN : 0); - append_completion(out, out_completion, out_desc, local_flags, match); + if (!match_acceptable || out == NULL) { + return match_acceptable; } + + // Wildcard complete. + 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)); + wcstring out_completion = full_replacement ? params.orig : str + wcslen(wc); + 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. tab-completing + // 'foo' when a file 'foo' exists. + complete_flags_t local_flags = flags | (full_replacement ? COMPLETE_REPLACES_TOKEN : 0); + append_completion(out, out_completion, out_desc, local_flags, match); return match_acceptable; } 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 @@ -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. return false; } - default: { assert(0 && "Unreachable code reached"); } + default: { + DIE("Unreachable code reached"); + break; + } } assert(0 && "Unreachable code reached"); @@ -322,46 +327,44 @@ 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. static wcstring file_get_desc(const wcstring &filename, int lstat_res, const struct stat &lbuf, int stat_res, const struct stat &buf, int err) { - if (!lstat_res) { - if (S_ISLNK(lbuf.st_mode)) { - if (!stat_res) { - if (S_ISDIR(buf.st_mode)) { - return COMPLETE_DIRECTORY_SYMLINK_DESC; - } - 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 - // find out if we can actually execute a file using the result from - // stat. It is much safer to use the access function, since it tells us - // exactly what we want to know. - return COMPLETE_EXEC_LINK_DESC; - } + if (lstat_res) { + return COMPLETE_FILE_DESC; + } - return COMPLETE_SYMLINK_DESC; + if (S_ISLNK(lbuf.st_mode)) { + if (!stat_res) { + if (S_ISDIR(buf.st_mode)) { + return COMPLETE_DIRECTORY_SYMLINK_DESC; + } + 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 find out if + // we can actually execute a file using the result from stat. It is much safer to + // use the access function, since it tells us exactly what we want to know. + return COMPLETE_EXEC_LINK_DESC; } - if (err == ENOENT) return COMPLETE_ROTTEN_SYMLINK_DESC; - if (err == ELOOP) return COMPLETE_LOOP_SYMLINK_DESC; - // On unknown errors we do nothing. The file will be given the default 'File' - // description or one based on the suffix. - } else if (S_ISCHR(buf.st_mode)) { - return COMPLETE_CHAR_DESC; - } else if (S_ISBLK(buf.st_mode)) { - return COMPLETE_BLOCK_DESC; - } else if (S_ISFIFO(buf.st_mode)) { - return COMPLETE_FIFO_DESC; - } else if (S_ISSOCK(buf.st_mode)) { - return COMPLETE_SOCKET_DESC; - } else if (S_ISDIR(buf.st_mode)) { - return COMPLETE_DIRECTORY_DESC; - } else { - 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 actually execute a file using the result from stat. It is much - // safer to use the access function, since it tells us exactly what we want to - // know. - return COMPLETE_EXEC_DESC; - } + return COMPLETE_SYMLINK_DESC; } + + if (err == ENOENT) return COMPLETE_ROTTEN_SYMLINK_DESC; + if (err == ELOOP) return COMPLETE_LOOP_SYMLINK_DESC; + // On unknown errors we do nothing. The file will be given the default 'File' + // description or one based on the suffix. + } else if (S_ISCHR(buf.st_mode)) { + return COMPLETE_CHAR_DESC; + } else if (S_ISBLK(buf.st_mode)) { + return COMPLETE_BLOCK_DESC; + } else if (S_ISFIFO(buf.st_mode)) { + return COMPLETE_FIFO_DESC; + } else if (S_ISSOCK(buf.st_mode)) { + return COMPLETE_SOCKET_DESC; + } else if (S_ISDIR(buf.st_mode)) { + return COMPLETE_DIRECTORY_DESC; + } else 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 + // actually execute a file using the result from stat. It is much safer to use the access + // function, since it tells us exactly what we want to know. + return COMPLETE_EXEC_DESC; } return COMPLETE_FILE_DESC;