lint: too few branches in switch statement

Someone was way too enamored of the `switch` statement. Using it in
places where a simple `if...else if...else` was clearer and shorter.
This commit is contained in:
Kurtis Rader 2016-10-22 20:32:25 -07:00
parent 42458ff7ab
commit 21521b2953
13 changed files with 339 additions and 467 deletions

View file

@ -345,23 +345,16 @@ static int get_terminfo_sequence(const wchar_t *seq, wcstring *out_seq, io_strea
if (input_terminfo_get_sequence(seq, out_seq)) {
return 1;
}
wcstring eseq = escape_string(seq, 0);
switch (errno) {
case ENOENT: {
streams.err.append_format(_(L"%ls: No key with name '%ls' found\n"), L"bind",
eseq.c_str());
break;
}
case EILSEQ: {
if (errno == ENOENT) {
streams.err.append_format(_(L"%ls: No key with name '%ls' found\n"), L"bind", eseq.c_str());
} else if (errno == EILSEQ) {
streams.err.append_format(_(L"%ls: Key with name '%ls' does not have any mapping\n"),
L"bind", eseq.c_str());
break;
}
default: {
} else {
streams.err.append_format(_(L"%ls: Unknown error trying to bind to key named '%ls'\n"),
L"bind", eseq.c_str());
break;
}
}
return 0;
}
@ -525,12 +518,10 @@ static int builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv)
break;
}
case BIND_INSERT: {
switch (argc - w.woptind) {
case 0: {
int arg_count = argc - w.woptind;
if (arg_count == 0) {
builtin_bind_list(bind_mode_given ? bind_mode : NULL, streams);
break;
}
case 1: {
} else if (arg_count == 1) {
wcstring seq;
if (use_terminfo) {
if (!get_terminfo_sequence(argv[w.woptind], &seq, streams)) {
@ -548,21 +539,17 @@ static int builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv)
streams.err.append_format(_(L"%ls: No binding found for key '%ls'\n"),
argv[0], eseq.c_str());
} else {
streams.err.append_format(
_(L"%ls: No binding found for sequence '%ls'\n"), argv[0],
eseq.c_str());
streams.err.append_format(_(L"%ls: No binding found for sequence '%ls'\n"),
argv[0], eseq.c_str());
}
}
break;
}
default: {
} else {
if (builtin_bind_add(argv[w.woptind], argv + (w.woptind + 1),
argc - (w.woptind + 1), bind_mode, sets_bind_mode,
use_terminfo, streams)) {
res = STATUS_BUILTIN_ERROR;
}
break;
}
}
break;
}
@ -1776,8 +1763,8 @@ static int builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **arg
}
}
switch (argc - w.woptind) {
case 0: {
int arg_count = argc - w.woptind;
if (arg_count == 0) {
long res;
if (!seeded) {
seeded = 1;
@ -1785,31 +1772,25 @@ static int builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **arg
}
lrand48_r(&seed_buffer, &res);
streams.out.append_format(L"%ld\n", res % 32768);
break;
}
case 1: {
} else if (arg_count == 1) {
long foo;
wchar_t *end = 0;
errno = 0;
foo = wcstol(argv[w.woptind], &end, 10);
if (errno || *end) {
streams.err.append_format(_(L"%ls: Seed value '%ls' is not a valid number\n"),
argv[0], argv[w.woptind]);
streams.err.append_format(_(L"%ls: Seed value '%ls' is not a valid number\n"), argv[0],
argv[w.woptind]);
return STATUS_BUILTIN_ERROR;
}
seeded = 1;
srand48_r(foo, &seed_buffer);
break;
}
default: {
} else {
streams.err.append_format(_(L"%ls: Expected zero or one argument, got %d\n"), argv[0],
argc - w.woptind);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
}
return STATUS_BUILTIN_OK;
}
@ -1900,21 +1881,18 @@ static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv)
errno = 0;
nchars = fish_wcstoi(w.woptarg, &end, 10);
if (errno || *end != 0) {
switch (errno) {
case ERANGE: {
if (errno == ERANGE) {
streams.err.append_format(_(L"%ls: Argument '%ls' is out of range\n"),
argv[0], w.woptarg);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
default: {
streams.err.append_format(
_(L"%ls: Argument '%ls' must be an integer\n"), argv[0], w.woptarg);
} else {
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"),
argv[0], w.woptarg);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
}
}
break;
}
case 's': {
@ -2045,18 +2023,10 @@ static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv)
finished = 1;
} else {
size_t sz = mbrtowc(&res, &b, 1, &state);
switch (sz) {
case (size_t)-1: {
if (sz == (size_t)-1) {
memset(&state, 0, sizeof(state));
break;
}
case (size_t)-2: {
break;
}
default: {
} else if (sz != (size_t)-2) {
finished = 1;
break;
}
}
}
}
@ -2310,13 +2280,16 @@ static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **arg
static int builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc = builtin_count_args(argv);
long ec = 0;
switch (argc) {
case 1: {
ec = proc_get_last_status();
break;
if (argc > 2) {
streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
case 2: {
long ec;
if (argc == 1) {
ec = proc_get_last_status();
} else {
wchar_t *end;
errno = 0;
ec = wcstol(argv[1], &end, 10);
@ -2326,14 +2299,6 @@ static int builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv)
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
break;
}
default: {
streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
}
reader_exit(1, 0);
return (int)ec;
@ -2752,13 +2717,15 @@ static int builtin_breakpoint(parser_t &parser, io_streams_t &streams, wchar_t *
/// Function for handling the \c return builtin.
static int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc = builtin_count_args(argv);
int status = proc_get_last_status();
switch (argc) {
case 1: {
break;
if (argc > 2) {
streams.err.append_format(_(L"%ls: Too many arguments\n"), argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
case 2: {
int status;
if (argc == 2) {
wchar_t *end;
errno = 0;
status = fish_wcstoi(argv[1], &end, 10);
@ -2768,13 +2735,8 @@ static int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **arg
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
break;
}
default: {
streams.err.append_format(_(L"%ls: Too many arguments\n"), argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
} else {
status = proc_get_last_status();
}
// Find the function block.

View file

@ -152,15 +152,11 @@ static void write_part(const wchar_t *begin, const wchar_t *end, int cut_at_curs
while (tok.next(&token)) {
if ((cut_at_cursor) && (token.offset + token.text.size() >= pos)) break;
switch (token.type) {
case TOK_STRING: {
if (token.type == TOK_STRING) {
wcstring tmp = token.text;
unescape_string_in_place(&tmp, UNESCAPE_INCOMPLETE);
out.append(tmp);
out.push_back(L'\n');
break;
}
default: { break; }
}
}
@ -476,24 +472,18 @@ int builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv)
}
}
switch (argc - w.woptind) {
case 0: {
int arg_count = argc - w.woptind;
if (arg_count == 0) {
write_part(begin, end, cut_at_cursor, tokenize, streams);
break;
}
case 1: {
} else if (arg_count == 1) {
replace_part(begin, end, argv[w.woptind], append_mode);
break;
}
default: {
} else {
wcstring sb = argv[w.woptind];
for (int i = w.woptind + 1; i < argc; i++) {
sb.push_back(L'\n');
sb.append(argv[i]);
}
replace_part(begin, end, sb.c_str(), append_mode);
break;
}
}
return 0;

View file

@ -579,22 +579,17 @@ bool binary_primary::evaluate(wcstring_list_t &errors) {
}
bool unary_operator::evaluate(wcstring_list_t &errors) {
switch (token) {
case test_bang: {
if (token == test_bang) {
assert(subject.get());
return !subject->evaluate(errors);
}
default: {
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return false;
}
}
}
bool combining_expression::evaluate(wcstring_list_t &errors) {
switch (token) {
case test_combine_and:
case test_combine_or: {
if (token == test_combine_and || token == test_combine_or) {
assert(!subjects.empty());
assert(combiners.size() + 1 == subjects.size());
@ -630,12 +625,10 @@ bool combining_expression::evaluate(wcstring_list_t &errors) {
}
return or_result;
}
default: {
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return BUILTIN_TEST_FAIL;
}
}
}
bool parenthetical_expression::evaluate(wcstring_list_t &errors) {
return contents->evaluate(errors);
@ -798,16 +791,13 @@ int builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
// Collect the arguments into a list.
const wcstring_list_t args(argv + 1, argv + 1 + argc);
switch (argc) {
case 0: {
// Per 1003.1, exit false.
return BUILTIN_TEST_FAIL;
}
case 1: {
if (argc == 0) {
return BUILTIN_TEST_FAIL; // Per 1003.1, exit false.
} else if (argc == 1) {
// Per 1003.1, exit true if the arg is non-empty.
return args.at(0).empty() ? BUILTIN_TEST_FAIL : BUILTIN_TEST_SUCCESS;
}
default: {
// Try parsing. If expr is not nil, we are responsible for deleting it.
wcstring err;
expression *expr = test_parser::parse_args(args, err);
@ -834,6 +824,3 @@ int builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
delete expr;
return result ? BUILTIN_TEST_SUCCESS : BUILTIN_TEST_FAIL;
}
}
return 1;
}

View file

@ -278,12 +278,12 @@ int builtin_ulimit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
return 0;
}
switch (argc - w.woptind) {
case 0: { // show current limit value
int arg_count = argc - w.woptind;
if (arg_count == 0) {
// Show current limit value.
print(what, hard, streams);
break;
}
case 1: { // change current limit value
} else if (arg_count == 1) {
// Change current limit value.
rlim_t new_limit;
wchar_t *end;
@ -302,8 +302,7 @@ int builtin_ulimit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
errno = 0;
new_limit = wcstol(argv[w.woptind], &end, 10);
if (errno || *end) {
streams.err.append_format(L"%ls: Invalid limit '%ls'\n", argv[0],
argv[w.woptind]);
streams.err.append_format(L"%ls: Invalid limit '%ls'\n", argv[0], argv[w.woptind]);
builtin_print_help(parser, streams, argv[0], streams.err);
return 1;
}
@ -312,12 +311,9 @@ int builtin_ulimit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
return set(what, hard, soft, new_limit, streams);
}
default: {
streams.err.append(argv[0]);
streams.err.append(L": Too many arguments\n");
builtin_print_help(parser, streams, argv[0], streams.err);
return 1;
}
}
return 0;
}

View file

@ -166,12 +166,10 @@ wcstring expand_escape_variable(const wcstring &in) {
tokenize_variable_array(in, lst);
switch (lst.size()) {
case 0: {
int size = lst.size();
if (size == 0) {
buff.append(L"''");
break;
}
case 1: {
} else if (size == 1) {
const wcstring &el = lst.at(0);
if (el.find(L' ') != wcstring::npos && is_quotable(el)) {
@ -181,9 +179,7 @@ wcstring expand_escape_variable(const wcstring &in) {
} else {
buff.append(escape_string(el, 1));
}
break;
}
default: {
} else {
for (size_t j = 0; j < lst.size(); j++) {
const wcstring &el = lst.at(j);
if (j) buff.append(L" ");
@ -197,7 +193,6 @@ wcstring expand_escape_variable(const wcstring &in) {
}
}
}
}
return buff;
}

View file

@ -223,14 +223,8 @@ void input_set_bind_mode(const wcstring &bm) {
}
/// Returns the arity of a given input function.
int input_function_arity(int function) {
switch (function) {
case R_FORWARD_JUMP:
case R_BACKWARD_JUMP: {
return 1;
}
default: { return 0; }
}
static int input_function_arity(int function) {
return (function == R_FORWARD_JUMP || function == R_BACKWARD_JUMP) ? 1 : 0;
}
/// Sets the return status of the most recently executed input function.

View file

@ -110,27 +110,18 @@ static wint_t readb() {
res = select(fd_max + 1, &fdset, 0, 0, usecs_delay > 0 ? &tv : NULL);
if (res == -1) {
switch (errno) {
case EINTR:
case EAGAIN: {
if (errno == EINTR || errno == EAGAIN) {
if (interrupt_handler) {
int res = interrupt_handler();
if (res) {
return res;
}
if (has_lookahead()) {
return lookahead_pop();
}
if (res) return res;
if (has_lookahead()) return lookahead_pop();
}
do_loop = true;
break;
}
default: {
} else {
// The terminal has been closed. Save and exit.
return R_EOF;
}
}
} else {
// Assume we loop unless we see a character in stdin.
do_loop = true;

View file

@ -222,21 +222,15 @@ int iothread_port(void) {
void iothread_service_completion(void) {
ASSERT_IS_MAIN_THREAD();
char wakeup_byte = 0;
char wakeup_byte;
VOMIT_ON_FAILURE(1 != read_loop(iothread_port(), &wakeup_byte, sizeof wakeup_byte));
switch (wakeup_byte) {
case IO_SERVICE_MAIN_THREAD_REQUEST_QUEUE: {
if (wakeup_byte == IO_SERVICE_MAIN_THREAD_REQUEST_QUEUE) {
iothread_service_main_thread_requests();
break;
}
case IO_SERVICE_RESULT_QUEUE: {
} else if (wakeup_byte == IO_SERVICE_RESULT_QUEUE) {
iothread_service_result_queue();
break;
}
default: {
} else {
fprintf(stderr, "Unknown wakeup byte %02x in %s\n", wakeup_byte, __FUNCTION__);
break;
}
}
}

View file

@ -924,18 +924,13 @@ bool parse_ll_t::top_node_handle_terminal_types(parse_token_t token) {
// Now see if we actually matched
bool matched = false;
if (stack_top.type == token.type) {
switch (stack_top.type) {
case parse_token_type_string: {
if (stack_top.type == parse_token_type_string) {
// We matched if the keywords match, or no keyword was required.
matched = (stack_top.keyword == parse_keyword_none ||
stack_top.keyword == token.keyword);
break;
}
default: {
matched =
(stack_top.keyword == parse_keyword_none || stack_top.keyword == token.keyword);
} else {
// For other types, we only require that the types match.
matched = true;
break;
}
}
}
@ -1550,8 +1545,7 @@ enum parse_bool_statement_type_t parse_node_tree_t::statement_boolean_type(
bool parse_node_tree_t::job_should_be_backgrounded(const parse_node_t &job) const {
assert(job.type == symbol_job);
const parse_node_t *opt_background = get_child(job, 2, symbol_optional_background);
bool result = opt_background != NULL && opt_background->tag == parse_background;
return result;
return opt_background != NULL && opt_background->tag == parse_background;
}
const parse_node_t *parse_node_tree_t::next_node_in_node_list(

View file

@ -1019,9 +1019,7 @@ parser_test_error_bits_t parse_util_detect_errors_in_argument(const parse_node_t
// Check for invalid variable expansions.
const size_t unesc_size = unesc.size();
for (size_t idx = 0; idx < unesc_size; idx++) {
switch (unesc.at(idx)) {
case VARIABLE_EXPAND:
case VARIABLE_EXPAND_SINGLE: {
if (unesc.at(idx) == VARIABLE_EXPAND || unesc.at(idx) == VARIABLE_EXPAND_SINGLE) {
wchar_t next_char = idx + 1 < unesc_size ? unesc.at(idx + 1) : L'\0';
if (next_char != VARIABLE_EXPAND && next_char != VARIABLE_EXPAND_SINGLE &&
@ -1039,9 +1037,6 @@ parser_test_error_bits_t parse_util_detect_errors_in_argument(const parse_node_t
out_errors);
}
}
break;
}
}
}

View file

@ -190,16 +190,10 @@ wcstring path_apply_working_directory(const wcstring &path, const wcstring &work
// We're going to make sure that if we want to prepend the wd, that the string has no leading
// "/".
bool prepend_wd;
switch (path.at(0)) {
case L'/':
case HOME_DIRECTORY: {
if (path.at(0) == L'/' || path.at(0) == HOME_DIRECTORY) {
prepend_wd = false;
break;
}
default: {
} else {
prepend_wd = true;
break;
}
}
if (!prepend_wd) {

View file

@ -1379,15 +1379,13 @@ static bool handle_completions(const std::vector<completion_t> &comp,
const wcstring tok(begin, end - begin);
// Check trivial cases.
switch (comp.size()) {
case 0: {
int size = comp.size();
if (size == 0) {
// No suitable completions found, flash screen and return.
reader_flash();
done = true;
success = false;
break;
}
case 1: {
} else if (size == 1) {
// Exactly one suitable completion found - insert it.
const completion_t &c = comp.at(0);
@ -1398,8 +1396,6 @@ static bool handle_completions(const std::vector<completion_t> &comp,
}
done = true;
success = true;
break;
}
}
if (!done) {
@ -1790,8 +1786,7 @@ static void handle_token_history(int forward, int reset) {
tokenizer_t tok(data->token_history_buff.c_str(), TOK_ACCEPT_UNFINISHED);
tok_t token;
while (tok.next(&token)) {
switch (token.type) {
case TOK_STRING: {
if (token.type == TOK_STRING) {
if (token.text.find(data->search_buff) != wcstring::npos) {
// debug( 3, L"Found token at pos %d\n", tok_get_pos( &tok ) );
if (token.offset >= current_pos) {
@ -1799,16 +1794,15 @@ static void handle_token_history(int forward, int reset) {
}
// debug( 3, L"ok pos" );
if (find(data->search_prev.begin(), data->search_prev.end(),
token.text) == data->search_prev.end()) {
if (find(data->search_prev.begin(), data->search_prev.end(), token.text) ==
data->search_prev.end()) {
data->token_history_pos = token.offset;
str = token.text;
}
}
} else {
break;
}
default: { break; }
}
}
}
@ -2873,8 +2867,7 @@ const wchar_t *reader_readline(int nchars) {
data->history_search.skip_matches(skip_list);
}
switch (data->search_mode) {
case LINE_SEARCH: {
if (data->search_mode == LINE_SEARCH) {
if ((c == R_HISTORY_SEARCH_BACKWARD) ||
(c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) {
data->history_search.go_backwards();
@ -2891,19 +2884,14 @@ const wchar_t *reader_readline(int nchars) {
} else {
new_text = data->history_search.current_string();
}
set_command_line_and_position(&data->command_line, new_text,
new_text.size());
break;
}
case TOKEN_SEARCH: {
set_command_line_and_position(&data->command_line, new_text, new_text.size());
} else if (data->search_mode == TOKEN_SEARCH) {
if ((c == R_HISTORY_SEARCH_BACKWARD) ||
(c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) {
handle_token_history(SEARCH_BACKWARD, reset);
} else {
handle_token_history(SEARCH_FORWARD, reset);
}
break;
}
}
break;
}

View file

@ -339,18 +339,10 @@ static wcstring file_get_desc(const wcstring &filename, int lstat_res, const str
return COMPLETE_SYMLINK_DESC;
}
switch (err) {
case ENOENT: {
return COMPLETE_ROTTEN_SYMLINK_DESC;
}
case ELOOP: {
return COMPLETE_LOOP_SYMLINK_DESC;
}
default: {
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)) {