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)) { if (input_terminfo_get_sequence(seq, out_seq)) {
return 1; return 1;
} }
wcstring eseq = escape_string(seq, 0); wcstring eseq = escape_string(seq, 0);
switch (errno) { if (errno == ENOENT) {
case ENOENT: { streams.err.append_format(_(L"%ls: No key with name '%ls' found\n"), L"bind", eseq.c_str());
streams.err.append_format(_(L"%ls: No key with name '%ls' found\n"), L"bind", } else if (errno == EILSEQ) {
eseq.c_str());
break;
}
case EILSEQ: {
streams.err.append_format(_(L"%ls: Key with name '%ls' does not have any mapping\n"), streams.err.append_format(_(L"%ls: Key with name '%ls' does not have any mapping\n"),
L"bind", eseq.c_str()); L"bind", eseq.c_str());
break; } else {
}
default: {
streams.err.append_format(_(L"%ls: Unknown error trying to bind to key named '%ls'\n"), streams.err.append_format(_(L"%ls: Unknown error trying to bind to key named '%ls'\n"),
L"bind", eseq.c_str()); L"bind", eseq.c_str());
break;
}
} }
return 0; return 0;
} }
@ -525,12 +518,10 @@ static int builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv)
break; break;
} }
case BIND_INSERT: { case BIND_INSERT: {
switch (argc - w.woptind) { int arg_count = argc - w.woptind;
case 0: { if (arg_count == 0) {
builtin_bind_list(bind_mode_given ? bind_mode : NULL, streams); builtin_bind_list(bind_mode_given ? bind_mode : NULL, streams);
break; } else if (arg_count == 1) {
}
case 1: {
wcstring seq; wcstring seq;
if (use_terminfo) { if (use_terminfo) {
if (!get_terminfo_sequence(argv[w.woptind], &seq, streams)) { 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"), streams.err.append_format(_(L"%ls: No binding found for key '%ls'\n"),
argv[0], eseq.c_str()); argv[0], eseq.c_str());
} else { } else {
streams.err.append_format( streams.err.append_format(_(L"%ls: No binding found for sequence '%ls'\n"),
_(L"%ls: No binding found for sequence '%ls'\n"), argv[0], argv[0], eseq.c_str());
eseq.c_str());
} }
} }
break; break;
} } else {
default: {
if (builtin_bind_add(argv[w.woptind], argv + (w.woptind + 1), if (builtin_bind_add(argv[w.woptind], argv + (w.woptind + 1),
argc - (w.woptind + 1), bind_mode, sets_bind_mode, argc - (w.woptind + 1), bind_mode, sets_bind_mode,
use_terminfo, streams)) { use_terminfo, streams)) {
res = STATUS_BUILTIN_ERROR; res = STATUS_BUILTIN_ERROR;
} }
break;
}
} }
break; break;
} }
@ -1776,8 +1763,8 @@ static int builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **arg
} }
} }
switch (argc - w.woptind) { int arg_count = argc - w.woptind;
case 0: { if (arg_count == 0) {
long res; long res;
if (!seeded) { if (!seeded) {
seeded = 1; 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); lrand48_r(&seed_buffer, &res);
streams.out.append_format(L"%ld\n", res % 32768); streams.out.append_format(L"%ld\n", res % 32768);
break; } else if (arg_count == 1) {
}
case 1: {
long foo; long foo;
wchar_t *end = 0; wchar_t *end = 0;
errno = 0; errno = 0;
foo = wcstol(argv[w.woptind], &end, 10); foo = wcstol(argv[w.woptind], &end, 10);
if (errno || *end) { if (errno || *end) {
streams.err.append_format(_(L"%ls: Seed value '%ls' is not a valid number\n"), streams.err.append_format(_(L"%ls: Seed value '%ls' is not a valid number\n"), argv[0],
argv[0], argv[w.woptind]); argv[w.woptind]);
return STATUS_BUILTIN_ERROR; return STATUS_BUILTIN_ERROR;
} }
seeded = 1; seeded = 1;
srand48_r(foo, &seed_buffer); srand48_r(foo, &seed_buffer);
break; } else {
}
default: {
streams.err.append_format(_(L"%ls: Expected zero or one argument, got %d\n"), argv[0], streams.err.append_format(_(L"%ls: Expected zero or one argument, got %d\n"), argv[0],
argc - w.woptind); argc - w.woptind);
builtin_print_help(parser, streams, argv[0], streams.err); builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR; return STATUS_BUILTIN_ERROR;
} }
}
return STATUS_BUILTIN_OK; return STATUS_BUILTIN_OK;
} }
@ -1900,21 +1881,18 @@ static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv)
errno = 0; errno = 0;
nchars = fish_wcstoi(w.woptarg, &end, 10); nchars = fish_wcstoi(w.woptarg, &end, 10);
if (errno || *end != 0) { if (errno || *end != 0) {
switch (errno) { if (errno == ERANGE) {
case ERANGE: {
streams.err.append_format(_(L"%ls: Argument '%ls' is out of range\n"), streams.err.append_format(_(L"%ls: Argument '%ls' is out of range\n"),
argv[0], w.woptarg); argv[0], w.woptarg);
builtin_print_help(parser, streams, argv[0], streams.err); builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR; return STATUS_BUILTIN_ERROR;
} } else {
default: { streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"),
streams.err.append_format( argv[0], w.woptarg);
_(L"%ls: Argument '%ls' must be an integer\n"), argv[0], w.woptarg);
builtin_print_help(parser, streams, argv[0], streams.err); builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR; return STATUS_BUILTIN_ERROR;
} }
} }
}
break; break;
} }
case 's': { case 's': {
@ -2045,18 +2023,10 @@ static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv)
finished = 1; finished = 1;
} else { } else {
size_t sz = mbrtowc(&res, &b, 1, &state); size_t sz = mbrtowc(&res, &b, 1, &state);
switch (sz) { if (sz == (size_t)-1) {
case (size_t)-1: {
memset(&state, 0, sizeof(state)); memset(&state, 0, sizeof(state));
break; } else if (sz != (size_t)-2) {
}
case (size_t)-2: {
break;
}
default: {
finished = 1; 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) { static int builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc = builtin_count_args(argv); int argc = builtin_count_args(argv);
long ec = 0; if (argc > 2) {
switch (argc) { streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
case 1: { builtin_print_help(parser, streams, argv[0], streams.err);
ec = proc_get_last_status(); return STATUS_BUILTIN_ERROR;
break;
} }
case 2: {
long ec;
if (argc == 1) {
ec = proc_get_last_status();
} else {
wchar_t *end; wchar_t *end;
errno = 0; errno = 0;
ec = wcstol(argv[1], &end, 10); 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); builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR; 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); reader_exit(1, 0);
return (int)ec; 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. /// Function for handling the \c return builtin.
static int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv) { static int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc = builtin_count_args(argv); int argc = builtin_count_args(argv);
int status = proc_get_last_status();
switch (argc) { if (argc > 2) {
case 1: { streams.err.append_format(_(L"%ls: Too many arguments\n"), argv[0]);
break; builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
} }
case 2: {
int status;
if (argc == 2) {
wchar_t *end; wchar_t *end;
errno = 0; errno = 0;
status = fish_wcstoi(argv[1], &end, 10); 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); builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR; return STATUS_BUILTIN_ERROR;
} }
break; } else {
} status = proc_get_last_status();
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;
}
} }
// Find the function block. // 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)) { while (tok.next(&token)) {
if ((cut_at_cursor) && (token.offset + token.text.size() >= pos)) break; if ((cut_at_cursor) && (token.offset + token.text.size() >= pos)) break;
switch (token.type) { if (token.type == TOK_STRING) {
case TOK_STRING: {
wcstring tmp = token.text; wcstring tmp = token.text;
unescape_string_in_place(&tmp, UNESCAPE_INCOMPLETE); unescape_string_in_place(&tmp, UNESCAPE_INCOMPLETE);
out.append(tmp); out.append(tmp);
out.push_back(L'\n'); 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) { int arg_count = argc - w.woptind;
case 0: { if (arg_count == 0) {
write_part(begin, end, cut_at_cursor, tokenize, streams); write_part(begin, end, cut_at_cursor, tokenize, streams);
break; } else if (arg_count == 1) {
}
case 1: {
replace_part(begin, end, argv[w.woptind], append_mode); replace_part(begin, end, argv[w.woptind], append_mode);
break; } else {
}
default: {
wcstring sb = argv[w.woptind]; wcstring sb = argv[w.woptind];
for (int i = w.woptind + 1; i < argc; i++) { for (int i = w.woptind + 1; i < argc; i++) {
sb.push_back(L'\n'); sb.push_back(L'\n');
sb.append(argv[i]); sb.append(argv[i]);
} }
replace_part(begin, end, sb.c_str(), append_mode); replace_part(begin, end, sb.c_str(), append_mode);
break;
}
} }
return 0; return 0;

View file

@ -579,22 +579,17 @@ bool binary_primary::evaluate(wcstring_list_t &errors) {
} }
bool unary_operator::evaluate(wcstring_list_t &errors) { bool unary_operator::evaluate(wcstring_list_t &errors) {
switch (token) { if (token == test_bang) {
case test_bang: {
assert(subject.get()); assert(subject.get());
return !subject->evaluate(errors); return !subject->evaluate(errors);
} }
default: {
errors.push_back(format_string(L"Unknown token type in %s", __func__)); errors.push_back(format_string(L"Unknown token type in %s", __func__));
return false; return false;
} }
}
}
bool combining_expression::evaluate(wcstring_list_t &errors) { bool combining_expression::evaluate(wcstring_list_t &errors) {
switch (token) { if (token == test_combine_and || token == test_combine_or) {
case test_combine_and:
case test_combine_or: {
assert(!subjects.empty()); assert(!subjects.empty());
assert(combiners.size() + 1 == subjects.size()); assert(combiners.size() + 1 == subjects.size());
@ -630,12 +625,10 @@ bool combining_expression::evaluate(wcstring_list_t &errors) {
} }
return or_result; return or_result;
} }
default: {
errors.push_back(format_string(L"Unknown token type in %s", __func__)); errors.push_back(format_string(L"Unknown token type in %s", __func__));
return BUILTIN_TEST_FAIL; return BUILTIN_TEST_FAIL;
} }
}
}
bool parenthetical_expression::evaluate(wcstring_list_t &errors) { bool parenthetical_expression::evaluate(wcstring_list_t &errors) {
return contents->evaluate(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. // Collect the arguments into a list.
const wcstring_list_t args(argv + 1, argv + 1 + argc); const wcstring_list_t args(argv + 1, argv + 1 + argc);
switch (argc) { if (argc == 0) {
case 0: { return BUILTIN_TEST_FAIL; // Per 1003.1, exit false.
// Per 1003.1, exit false. } else if (argc == 1) {
return BUILTIN_TEST_FAIL;
}
case 1: {
// Per 1003.1, exit true if the arg is non-empty. // Per 1003.1, exit true if the arg is non-empty.
return args.at(0).empty() ? BUILTIN_TEST_FAIL : BUILTIN_TEST_SUCCESS; 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. // Try parsing. If expr is not nil, we are responsible for deleting it.
wcstring err; wcstring err;
expression *expr = test_parser::parse_args(args, 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; delete expr;
return result ? BUILTIN_TEST_SUCCESS : BUILTIN_TEST_FAIL; 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; return 0;
} }
switch (argc - w.woptind) { int arg_count = argc - w.woptind;
case 0: { // show current limit value if (arg_count == 0) {
// Show current limit value.
print(what, hard, streams); print(what, hard, streams);
break; } else if (arg_count == 1) {
} // Change current limit value.
case 1: { // change current limit value
rlim_t new_limit; rlim_t new_limit;
wchar_t *end; wchar_t *end;
@ -302,8 +302,7 @@ int builtin_ulimit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
errno = 0; errno = 0;
new_limit = wcstol(argv[w.woptind], &end, 10); new_limit = wcstol(argv[w.woptind], &end, 10);
if (errno || *end) { if (errno || *end) {
streams.err.append_format(L"%ls: Invalid limit '%ls'\n", argv[0], streams.err.append_format(L"%ls: Invalid limit '%ls'\n", argv[0], argv[w.woptind]);
argv[w.woptind]);
builtin_print_help(parser, streams, argv[0], streams.err); builtin_print_help(parser, streams, argv[0], streams.err);
return 1; 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); return set(what, hard, soft, new_limit, streams);
} }
default: {
streams.err.append(argv[0]); streams.err.append(argv[0]);
streams.err.append(L": Too many arguments\n"); streams.err.append(L": Too many arguments\n");
builtin_print_help(parser, streams, argv[0], streams.err); builtin_print_help(parser, streams, argv[0], streams.err);
return 1; return 1;
} }
}
return 0;
}

View file

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

View file

@ -223,14 +223,8 @@ void input_set_bind_mode(const wcstring &bm) {
} }
/// Returns the arity of a given input function. /// Returns the arity of a given input function.
int input_function_arity(int function) { static int input_function_arity(int function) {
switch (function) { return (function == R_FORWARD_JUMP || function == R_BACKWARD_JUMP) ? 1 : 0;
case R_FORWARD_JUMP:
case R_BACKWARD_JUMP: {
return 1;
}
default: { return 0; }
}
} }
/// Sets the return status of the most recently executed input function. /// 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); res = select(fd_max + 1, &fdset, 0, 0, usecs_delay > 0 ? &tv : NULL);
if (res == -1) { if (res == -1) {
switch (errno) { if (errno == EINTR || errno == EAGAIN) {
case EINTR:
case EAGAIN: {
if (interrupt_handler) { if (interrupt_handler) {
int res = interrupt_handler(); int res = interrupt_handler();
if (res) { if (res) return res;
return res; if (has_lookahead()) return lookahead_pop();
}
if (has_lookahead()) {
return lookahead_pop();
}
} }
do_loop = true; do_loop = true;
break; } else {
}
default: {
// The terminal has been closed. Save and exit. // The terminal has been closed. Save and exit.
return R_EOF; return R_EOF;
} }
}
} else { } else {
// Assume we loop unless we see a character in stdin. // Assume we loop unless we see a character in stdin.
do_loop = true; do_loop = true;

View file

@ -222,21 +222,15 @@ int iothread_port(void) {
void iothread_service_completion(void) { void iothread_service_completion(void) {
ASSERT_IS_MAIN_THREAD(); ASSERT_IS_MAIN_THREAD();
char wakeup_byte = 0; char wakeup_byte;
VOMIT_ON_FAILURE(1 != read_loop(iothread_port(), &wakeup_byte, sizeof wakeup_byte)); VOMIT_ON_FAILURE(1 != read_loop(iothread_port(), &wakeup_byte, sizeof wakeup_byte));
switch (wakeup_byte) { if (wakeup_byte == IO_SERVICE_MAIN_THREAD_REQUEST_QUEUE) {
case IO_SERVICE_MAIN_THREAD_REQUEST_QUEUE: {
iothread_service_main_thread_requests(); iothread_service_main_thread_requests();
break; } else if (wakeup_byte == IO_SERVICE_RESULT_QUEUE) {
}
case IO_SERVICE_RESULT_QUEUE: {
iothread_service_result_queue(); iothread_service_result_queue();
break; } else {
}
default: {
fprintf(stderr, "Unknown wakeup byte %02x in %s\n", wakeup_byte, __FUNCTION__); 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 // Now see if we actually matched
bool matched = false; bool matched = false;
if (stack_top.type == token.type) { if (stack_top.type == token.type) {
switch (stack_top.type) { if (stack_top.type == parse_token_type_string) {
case parse_token_type_string: {
// We matched if the keywords match, or no keyword was required. // We matched if the keywords match, or no keyword was required.
matched = (stack_top.keyword == parse_keyword_none || matched =
stack_top.keyword == token.keyword); (stack_top.keyword == parse_keyword_none || stack_top.keyword == token.keyword);
break; } else {
}
default: {
// For other types, we only require that the types match. // For other types, we only require that the types match.
matched = true; 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 { bool parse_node_tree_t::job_should_be_backgrounded(const parse_node_t &job) const {
assert(job.type == symbol_job); assert(job.type == symbol_job);
const parse_node_t *opt_background = get_child(job, 2, symbol_optional_background); const parse_node_t *opt_background = get_child(job, 2, symbol_optional_background);
bool result = opt_background != NULL && opt_background->tag == parse_background; return opt_background != NULL && opt_background->tag == parse_background;
return result;
} }
const parse_node_t *parse_node_tree_t::next_node_in_node_list( 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. // Check for invalid variable expansions.
const size_t unesc_size = unesc.size(); const size_t unesc_size = unesc.size();
for (size_t idx = 0; idx < unesc_size; idx++) { for (size_t idx = 0; idx < unesc_size; idx++) {
switch (unesc.at(idx)) { if (unesc.at(idx) == VARIABLE_EXPAND || unesc.at(idx) == VARIABLE_EXPAND_SINGLE) {
case VARIABLE_EXPAND:
case VARIABLE_EXPAND_SINGLE: {
wchar_t next_char = idx + 1 < unesc_size ? unesc.at(idx + 1) : L'\0'; wchar_t next_char = idx + 1 < unesc_size ? unesc.at(idx + 1) : L'\0';
if (next_char != VARIABLE_EXPAND && next_char != VARIABLE_EXPAND_SINGLE && 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); 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 // We're going to make sure that if we want to prepend the wd, that the string has no leading
// "/". // "/".
bool prepend_wd; bool prepend_wd;
switch (path.at(0)) { if (path.at(0) == L'/' || path.at(0) == HOME_DIRECTORY) {
case L'/':
case HOME_DIRECTORY: {
prepend_wd = false; prepend_wd = false;
break; } else {
}
default: {
prepend_wd = true; prepend_wd = true;
break;
}
} }
if (!prepend_wd) { 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); const wcstring tok(begin, end - begin);
// Check trivial cases. // Check trivial cases.
switch (comp.size()) { int size = comp.size();
case 0: { if (size == 0) {
// No suitable completions found, flash screen and return. // No suitable completions found, flash screen and return.
reader_flash(); reader_flash();
done = true; done = true;
success = false; success = false;
break; } else if (size == 1) {
}
case 1: {
// Exactly one suitable completion found - insert it. // Exactly one suitable completion found - insert it.
const completion_t &c = comp.at(0); const completion_t &c = comp.at(0);
@ -1398,8 +1396,6 @@ static bool handle_completions(const std::vector<completion_t> &comp,
} }
done = true; done = true;
success = true; success = true;
break;
}
} }
if (!done) { 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); tokenizer_t tok(data->token_history_buff.c_str(), TOK_ACCEPT_UNFINISHED);
tok_t token; tok_t token;
while (tok.next(&token)) { while (tok.next(&token)) {
switch (token.type) { if (token.type == TOK_STRING) {
case TOK_STRING: {
if (token.text.find(data->search_buff) != wcstring::npos) { if (token.text.find(data->search_buff) != wcstring::npos) {
// debug( 3, L"Found token at pos %d\n", tok_get_pos( &tok ) ); // debug( 3, L"Found token at pos %d\n", tok_get_pos( &tok ) );
if (token.offset >= current_pos) { if (token.offset >= current_pos) {
@ -1799,16 +1794,15 @@ static void handle_token_history(int forward, int reset) {
} }
// debug( 3, L"ok pos" ); // debug( 3, L"ok pos" );
if (find(data->search_prev.begin(), data->search_prev.end(), if (find(data->search_prev.begin(), data->search_prev.end(), token.text) ==
token.text) == data->search_prev.end()) { data->search_prev.end()) {
data->token_history_pos = token.offset; data->token_history_pos = token.offset;
str = token.text; str = token.text;
} }
} }
} else {
break; break;
} }
default: { break; }
}
} }
} }
@ -2873,8 +2867,7 @@ const wchar_t *reader_readline(int nchars) {
data->history_search.skip_matches(skip_list); data->history_search.skip_matches(skip_list);
} }
switch (data->search_mode) { if (data->search_mode == LINE_SEARCH) {
case LINE_SEARCH: {
if ((c == R_HISTORY_SEARCH_BACKWARD) || if ((c == R_HISTORY_SEARCH_BACKWARD) ||
(c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) { (c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) {
data->history_search.go_backwards(); data->history_search.go_backwards();
@ -2891,19 +2884,14 @@ const wchar_t *reader_readline(int nchars) {
} else { } else {
new_text = data->history_search.current_string(); new_text = data->history_search.current_string();
} }
set_command_line_and_position(&data->command_line, new_text, set_command_line_and_position(&data->command_line, new_text, new_text.size());
new_text.size()); } else if (data->search_mode == TOKEN_SEARCH) {
break;
}
case TOKEN_SEARCH: {
if ((c == R_HISTORY_SEARCH_BACKWARD) || if ((c == R_HISTORY_SEARCH_BACKWARD) ||
(c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) { (c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) {
handle_token_history(SEARCH_BACKWARD, reset); handle_token_history(SEARCH_BACKWARD, reset);
} else { } else {
handle_token_history(SEARCH_FORWARD, reset); handle_token_history(SEARCH_FORWARD, reset);
} }
break;
}
} }
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; return COMPLETE_SYMLINK_DESC;
} }
switch (err) { if (err == ENOENT) return COMPLETE_ROTTEN_SYMLINK_DESC;
case ENOENT: { if (err == ELOOP) return COMPLETE_LOOP_SYMLINK_DESC;
return COMPLETE_ROTTEN_SYMLINK_DESC;
}
case ELOOP: {
return COMPLETE_LOOP_SYMLINK_DESC;
}
default: {
// On unknown errors we do nothing. The file will be given the default 'File' // On unknown errors we do nothing. The file will be given the default 'File'
// description or one based on the suffix. // description or one based on the suffix.
}
}
} else if (S_ISCHR(buf.st_mode)) { } else if (S_ISCHR(buf.st_mode)) {
return COMPLETE_CHAR_DESC; return COMPLETE_CHAR_DESC;
} else if (S_ISBLK(buf.st_mode)) { } else if (S_ISBLK(buf.st_mode)) {