diff --git a/src/builtin.cpp b/src/builtin.cpp index 7efd57e3b..e9018a833 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -452,7 +452,6 @@ static int builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv) while (1) { int opt_index = 0; int opt = w.wgetopt_long_only(argc, argv, L"aehkKfM:m:", long_options, &opt_index); - if (opt == -1) break; switch (opt) { @@ -501,6 +500,10 @@ static int builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv) builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return STATUS_BUILTIN_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -625,6 +628,10 @@ static int builtin_block(parser_t &parser, io_streams_t &streams, wchar_t **argv builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return STATUS_BUILTIN_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -665,6 +672,10 @@ static int builtin_block(parser_t &parser, io_streams_t &streams, wchar_t **argv block = parser.block_at_index(++block_idx); } } + default: { + DIE("unexpected scope"); + break; + } } if (block) { block->event_blocks.push_front(eb); @@ -713,6 +724,10 @@ static int builtin_builtin(parser_t &parser, io_streams_t &streams, wchar_t **ar builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return STATUS_BUILTIN_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -759,6 +774,10 @@ static int builtin_emit(parser_t &parser, io_streams_t &streams, wchar_t **argv) builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return STATUS_BUILTIN_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -812,6 +831,10 @@ static int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **ar builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return STATUS_BUILTIN_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -870,6 +893,10 @@ static int builtin_generic(parser_t &parser, io_streams_t &streams, wchar_t **ar builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return STATUS_BUILTIN_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -934,6 +961,7 @@ static wcstring functions_def(const wcstring &name) { append_format(out, L" --on-event %ls", next->str_param1.c_str()); break; } + default: { DIE("unexpected next->type"); } } } @@ -1048,6 +1076,10 @@ static int builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t ** builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return STATUS_BUILTIN_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -1193,9 +1225,11 @@ static unsigned int builtin_echo_digit(wchar_t wc, unsigned int base) { case L'7': { return 7; } + default: { break; } } - if (base == 16) switch (wc) { + if (base == 16) { + switch (wc) { case L'8': { return 8; } @@ -1226,7 +1260,10 @@ static unsigned int builtin_echo_digit(wchar_t wc, unsigned int base) { case L'F': { return 15; } + default: { break; } } + } + return UINT_MAX; } @@ -1642,6 +1679,10 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis res = 1; break; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -1760,6 +1801,10 @@ static int builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **arg builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return STATUS_BUILTIN_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -1915,6 +1960,10 @@ static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return STATUS_BUILTIN_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -2125,7 +2174,6 @@ static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **arg }; int mode = NORMAL; - int argc = builtin_count_args(argv); int res = STATUS_BUILTIN_OK; @@ -2213,11 +2261,18 @@ static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **arg builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return STATUS_BUILTIN_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } if (!res) { switch (mode) { + case DONE: { + break; + } case CURRENT_FILENAME: { const wchar_t *fn = parser.current_filename(); @@ -2270,6 +2325,7 @@ static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **arg streams.out.append(parser.stack_trace()); break; } + default: { break; } } } @@ -2426,6 +2482,10 @@ static int builtin_contains(parser_t &parser, io_streams_t &streams, wchar_t **a should_output_index = true; break; } + default: { + DIE("unexpected opt"); + break; + } } } diff --git a/src/builtin_commandline.cpp b/src/builtin_commandline.cpp index 1836b2377..9941a62cc 100644 --- a/src/builtin_commandline.cpp +++ b/src/builtin_commandline.cpp @@ -127,6 +127,10 @@ static void replace_part(const wchar_t *begin, const wchar_t *end, const wchar_t out_pos += wcslen(insert); break; } + default: { + DIE("unexpected append_mode"); + break; + } } out.append(end); reader_set_buffer(out, out_pos); @@ -326,6 +330,10 @@ int builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv) builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return 1; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -470,6 +478,10 @@ int builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv) parse_util_token_extent(get_buffer(), get_cursor_pos(), &begin, &end, 0, 0); break; } + default: { + DIE("unexpected buffer_part"); + break; + } } int arg_count = argc - w.woptind; diff --git a/src/builtin_complete.cpp b/src/builtin_complete.cpp index ec14f8ffe..489ff3c91 100644 --- a/src/builtin_complete.cpp +++ b/src/builtin_complete.cpp @@ -283,6 +283,10 @@ int builtin_complete(parser_t &parser, io_streams_t &streams, wchar_t **argv) { res = true; break; } + default: { + DIE("unexpected opt"); + break; + } } } diff --git a/src/builtin_jobs.cpp b/src/builtin_jobs.cpp index 72765cb82..7a92e6b9e 100644 --- a/src/builtin_jobs.cpp +++ b/src/builtin_jobs.cpp @@ -101,6 +101,10 @@ static void builtin_jobs_print(const job_t *j, int mode, int header, io_streams_ } break; } + default: { + DIE("unexpected mode"); + break; + } } } @@ -159,6 +163,10 @@ int builtin_jobs(parser_t &parser, io_streams_t &streams, wchar_t **argv) { builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return 1; } + default: { + DIE("unexpected opt"); + break; + } } } diff --git a/src/builtin_printf.cpp b/src/builtin_printf.cpp index 7442673fe..d43927250 100644 --- a/src/builtin_printf.cpp +++ b/src/builtin_printf.cpp @@ -541,6 +541,12 @@ void builtin_printf_state_t::print_direc(const wchar_t *start, size_t length, wc } break; } + default: { + // TODO: Determine if this should call DIE() + // WTF + DIE("unexpected opt"); + break; + } } } diff --git a/src/builtin_set.cpp b/src/builtin_set.cpp index 0427427ec..643a08bd0 100644 --- a/src/builtin_set.cpp +++ b/src/builtin_set.cpp @@ -123,6 +123,9 @@ static int my_env_set(const wchar_t *key, const wcstring_list_t &val, int scope, } switch (env_set(key, val_str, scope | ENV_USER)) { + case ENV_OK: { + break; + } case ENV_PERM: { streams.err.append_format(_(L"%ls: Tried to change the read-only variable '%ls'\n"), L"set", key); @@ -143,6 +146,10 @@ static int my_env_set(const wchar_t *key, const wcstring_list_t &val, int scope, retcode = 1; break; } + default: { + DIE("unexpected env_set() ret val"); + break; + } } return retcode; diff --git a/src/builtin_set_color.cpp b/src/builtin_set_color.cpp index b12c39229..b05bf016d 100644 --- a/src/builtin_set_color.cpp +++ b/src/builtin_set_color.cpp @@ -77,13 +77,13 @@ int builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t **argv) { // Parse options to obtain the requested operation and the modifiers. w.woptind = 0; while (1) { - int c = w.wgetopt_long(argc, argv, short_options, long_options, 0); + int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0); - if (c == -1) { + if (opt == -1) { break; } - switch (c) { + switch (opt) { case 0: { break; } @@ -110,6 +110,10 @@ int builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t **argv) { case '?': { return STATUS_BUILTIN_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } diff --git a/src/builtin_string.cpp b/src/builtin_string.cpp index 0f01096df..eb8e7e2f1 100644 --- a/src/builtin_string.cpp +++ b/src/builtin_string.cpp @@ -100,12 +100,13 @@ static int string_escape(parser_t &parser, io_streams_t &streams, int argc, wcha escape_flags_t flags = ESCAPE_ALL; wgetopter_t w; for (;;) { - int c = w.wgetopt_long(argc, argv, short_options, long_options, 0); + int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0); - if (c == -1) { + if (opt == -1) { break; } - switch (c) { + + switch (opt) { case 0: { break; } @@ -117,6 +118,10 @@ static int string_escape(parser_t &parser, io_streams_t &streams, int argc, wcha string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return BUILTIN_STRING_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -145,11 +150,13 @@ static int string_join(parser_t &parser, io_streams_t &streams, int argc, wchar_ bool quiet = false; wgetopter_t w; for (;;) { - int c = w.wgetopt_long(argc, argv, short_options, long_options, 0); - if (c == -1) { + int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0); + + if (opt == -1) { break; } - switch (c) { + + switch (opt) { case 0: { break; } @@ -161,6 +168,10 @@ static int string_join(parser_t &parser, io_streams_t &streams, int argc, wchar_ string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return BUILTIN_STRING_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -202,12 +213,12 @@ static int string_length(parser_t &parser, io_streams_t &streams, int argc, wcha bool quiet = false; wgetopter_t w; for (;;) { - int c = w.wgetopt_long(argc, argv, short_options, long_options, 0); + int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0); - if (c == -1) { + if (opt == -1) { break; } - switch (c) { + switch (opt) { case 0: { break; } @@ -219,6 +230,10 @@ static int string_length(parser_t &parser, io_streams_t &streams, int argc, wcha string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return BUILTIN_STRING_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -499,12 +514,12 @@ static int string_match(parser_t &parser, io_streams_t &streams, int argc, wchar bool regex = false; wgetopter_t w; for (;;) { - int c = w.wgetopt_long(argc, argv, short_options, long_options, 0); + int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0); - if (c == -1) { + if (opt == -1) { break; } - switch (c) { + switch (opt) { case 0: { break; } @@ -536,6 +551,10 @@ static int string_match(parser_t &parser, io_streams_t &streams, int argc, wchar string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return BUILTIN_STRING_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -731,12 +750,12 @@ static int string_replace(parser_t &parser, io_streams_t &streams, int argc, wch bool regex = false; wgetopter_t w; for (;;) { - int c = w.wgetopt_long(argc, argv, short_options, long_options, 0); + int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0); - if (c == -1) { + if (opt == -1) { break; } - switch (c) { + switch (opt) { case 0: { break; } @@ -760,6 +779,10 @@ static int string_replace(parser_t &parser, io_streams_t &streams, int argc, wch string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return BUILTIN_STRING_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -876,6 +899,10 @@ static int string_split(parser_t &parser, io_streams_t &streams, int argc, wchar string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return BUILTIN_STRING_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -989,6 +1016,10 @@ static int string_sub(parser_t &parser, io_streams_t &streams, int argc, wchar_t string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return BUILTIN_STRING_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } @@ -1078,6 +1109,10 @@ static int string_trim(parser_t &parser, io_streams_t &streams, int argc, wchar_ string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return BUILTIN_STRING_ERROR; } + default: { + DIE("unexpected opt"); + break; + } } } diff --git a/src/builtin_ulimit.cpp b/src/builtin_ulimit.cpp index 6c67eb9f1..5cc752982 100644 --- a/src/builtin_ulimit.cpp +++ b/src/builtin_ulimit.cpp @@ -262,6 +262,10 @@ int builtin_ulimit(parser_t &parser, io_streams_t &streams, wchar_t **argv) { builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return 1; } + default: { + DIE("unexpected opt"); + break; + } } } diff --git a/src/common.cpp b/src/common.cpp index d5bd5ee38..0fa22b310 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1220,6 +1220,7 @@ static bool unescape_string_internal(const wchar_t *const input, const size_t in to_append_or_none = unescape_special ? INTERNAL_SEPARATOR : NOT_A_WCHAR; break; } + default: { break; } } } else if (mode == mode_single_quotes) { if (c == L'\\') { @@ -1297,6 +1298,7 @@ static bool unescape_string_internal(const wchar_t *const input, const size_t in } break; } + default: { break; } } } diff --git a/src/common.h b/src/common.h index 3afae7a6c..33781f680 100644 --- a/src/common.h +++ b/src/common.h @@ -211,6 +211,9 @@ extern bool has_working_tty_timestamps; } /// Pause for input, then exit the program. If supported, print a backtrace first. +// The `return` will never be run but silences oclint warnings. Especially when this is called +// from within a `switch` block. As of the time I'm writing this oclint doesn't recognize the +// `__attribute__((noreturn))` on the exit_without_destructors() function. #define FATAL_EXIT() \ { \ char exit_read_buff; \ diff --git a/src/env.cpp b/src/env.cpp index 75784624c..2ac7fe2ba 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -476,6 +476,25 @@ static env_node_t *env_get_node(const wcstring &key) { return env; } +/// Set the value of the environment variable whose name matches key to val. +/// +/// Memory policy: All keys and values are copied, the parameters can and should be freed by the +/// caller afterwards +/// +/// \param key The key +/// \param val The value +/// \param mode The type of the variable. Can be any combination of ENV_GLOBAL, ENV_LOCAL, +/// ENV_EXPORT and ENV_USER. If mode is zero, the current variable space is searched and the current +/// mode is used. If no current variable with the same name is found, ENV_LOCAL is assumed. +/// +/// Returns: +/// +/// * ENV_OK on success. +/// * ENV_PERM, can only be returned when setting as a user, e.g. ENV_USER is set. This means that +/// the user tried to change a read-only variable. +/// * ENV_SCOPE, the variable cannot be set in the given scope. This applies to readonly/electric +/// variables set from the local or universal scopes, or set as exported. +/// * ENV_INVALID, the variable value was invalid. This applies only to special variables. int env_set(const wcstring &key, const wchar_t *val, env_mode_flags_t var_mode) { ASSERT_IS_MAIN_THREAD(); bool has_changed_old = has_changed_exported; @@ -512,7 +531,7 @@ int env_set(const wcstring &key, const wchar_t *val, env_mode_flags_t var_mode) umask(mask); // Do not actually create a umask variable, on env_get, it will be calculated // dynamically. - return 0; + return ENV_OK; } } @@ -636,7 +655,7 @@ int env_set(const wcstring &key, const wchar_t *val, env_mode_flags_t var_mode) // debug( 1, L"env_set: return from event firing" ); react_to_variable_change(key); - return 0; + return ENV_OK; } /// Attempt to remove/free the specified key/value pair from the specified map. diff --git a/src/env.h b/src/env.h index 6f783312c..56a84ffde 100644 --- a/src/env.h +++ b/src/env.h @@ -36,8 +36,8 @@ enum { }; typedef uint32_t env_mode_flags_t; -/// Error code for trying to alter read-only variable. -enum { ENV_PERM = 1, ENV_SCOPE, ENV_INVALID }; +/// Return values for `env_set()`. +enum { ENV_OK, ENV_PERM, ENV_SCOPE, ENV_INVALID }; /// A struct of configuration directories, determined in main() that fish will optionally pass to /// env_init. @@ -51,26 +51,6 @@ struct config_paths_t { /// Initialize environment variable data. void env_init(const struct config_paths_t *paths = NULL); -/// Set the value of the environment variable whose name matches key to val. -/// -/// Memory policy: All keys and values are copied, the parameters can and should be freed by the -/// caller afterwards -/// -/// \param key The key -/// \param val The value -/// \param mode The type of the variable. Can be any combination of ENV_GLOBAL, ENV_LOCAL, -/// ENV_EXPORT and ENV_USER. If mode is zero, the current variable space is searched and the current -/// mode is used. If no current variable with the same name is found, ENV_LOCAL is assumed. -/// -/// \returns 0 on success or an error code on failiure. -/// -/// The current error codes are: -/// -/// * ENV_PERM, can only be returned when setting as a user, e.g. ENV_USER is set. This means that -/// the user tried to change a read-only variable. -/// * ENV_SCOPE, the variable cannot be set in the given scope. This applies to readonly/electric -/// variables set from the local or universal scopes, or set as exported. -/// * ENV_INVALID, the variable value was invalid. This applies only to special variables. int env_set(const wcstring &key, const wchar_t *val, env_mode_flags_t mode); class env_var_t : public wcstring { diff --git a/src/event.cpp b/src/event.cpp index 143992c7e..c215d81fd 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -88,6 +88,10 @@ static int event_match(const event_t &classv, const event_t &instance) { case EVENT_GENERIC: { return instance.str_param1 == classv.str_param1; } + default: { + DIE("unexpected classv.type"); + break; + } } // This should never be reached. diff --git a/src/parse_execution.cpp b/src/parse_execution.cpp index 69bd85632..8887c682a 100644 --- a/src/parse_execution.cpp +++ b/src/parse_execution.cpp @@ -534,6 +534,10 @@ parse_execution_result_t parse_execution_context_t::run_switch_statement( case EXPAND_OK: { break; } + default: { + DIE("unexpected expand_string() return value"); + break; + } } if (result == parse_execution_success && switch_values_expanded.size() != 1) { @@ -945,6 +949,10 @@ parse_execution_result_t parse_execution_context_t::determine_arguments( case EXPAND_OK: { break; } + default: { + DIE("unexpected expand_string() return value"); + break; + } } // Now copy over any expanded arguments. Do it using swap() to avoid extra allocations; this diff --git a/src/parse_util.cpp b/src/parse_util.cpp index f0a7f37c5..80c89e160 100644 --- a/src/parse_util.cpp +++ b/src/parse_util.cpp @@ -1004,6 +1004,10 @@ parser_test_error_bits_t parse_util_detect_errors_in_argument(const parse_node_t } break; } + default: { + DIE("unexpected parse_util_locate_cmdsubst() return value"); + break; + } } } diff --git a/src/utf8.cpp b/src/utf8.cpp index 0e87e50ab..53d994852 100644 --- a/src/utf8.cpp +++ b/src/utf8.cpp @@ -20,6 +20,7 @@ #include #include +#include "common.h" #include "utf8.h" #define _NXT 0x80 @@ -137,9 +138,8 @@ static int __utf8_forbitten(unsigned char octet) { case 0xff: { return -1; } + default: { return 0; } } - - return 0; } /// This function translates UTF-8 string into UCS-2 or UCS-4 string (all symbols will be in local @@ -288,18 +288,17 @@ static size_t wchar_to_utf8_internal(const utf8_wchar_t *in, size_t insize, char if ((flags & UTF8_IGNORE_ERROR) == 0) return 0; continue; } - if (w_wide <= 0x0000007f) + if (w_wide <= 0x0000007f) { n = 1; - else if (w_wide <= 0x000007ff) + } else if (w_wide <= 0x000007ff) { n = 2; - else if (w_wide <= 0x0000ffff) + } else if (w_wide <= 0x0000ffff) { n = 3; - else if (w_wide <= 0x001fffff) + } else if (w_wide <= 0x001fffff) { n = 4; - else if (w_wide <= 0x03ffffff) - n = 5; - else - n = 6; /// if (w_wide <= 0x7fffffff) + } else { + DIE("invalid wide char"); + } total += n; @@ -340,10 +339,14 @@ static size_t wchar_to_utf8_internal(const utf8_wchar_t *in, size_t insize, char p[0] = _SEQ4 | ((oc[1] & 0x1f) >> 2); break; } + default: { + DIE("unexpected utff8 len"); + break; + } } - // NOTE: do not check here for forbitten UTF-8 characters. They cannot appear here because - // we do proper convertion. + // NOTE: do not check here for forbidden UTF-8 characters. They cannot appear here because + // we do proper conversion. p += n; }