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: {
streams.err.append_format(_(L"%ls: Key with name '%ls' does not have any mapping\n"),
L"bind", eseq.c_str());
break;
}
default: {
streams.err.append_format(_(L"%ls: Unknown error trying to bind to key named '%ls'\n"),
L"bind", eseq.c_str());
break;
}
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());
} else {
streams.err.append_format(_(L"%ls: Unknown error trying to bind to key named '%ls'\n"),
L"bind", eseq.c_str());
}
return 0;
}
@ -525,43 +518,37 @@ static int builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv)
break;
}
case BIND_INSERT: {
switch (argc - w.woptind) {
case 0: {
builtin_bind_list(bind_mode_given ? bind_mode : NULL, streams);
break;
int arg_count = argc - w.woptind;
if (arg_count == 0) {
builtin_bind_list(bind_mode_given ? bind_mode : NULL, streams);
} else if (arg_count == 1) {
wcstring seq;
if (use_terminfo) {
if (!get_terminfo_sequence(argv[w.woptind], &seq, streams)) {
res = STATUS_BUILTIN_ERROR;
// get_terminfo_sequence already printed the error.
break;
}
} else {
seq = argv[w.woptind];
}
case 1: {
wcstring seq;
if (!builtin_bind_list_one(seq, bind_mode, streams)) {
res = STATUS_BUILTIN_ERROR;
wcstring eseq = escape_string(argv[w.woptind], 0);
if (use_terminfo) {
if (!get_terminfo_sequence(argv[w.woptind], &seq, streams)) {
res = STATUS_BUILTIN_ERROR;
// get_terminfo_sequence already printed the error.
break;
}
streams.err.append_format(_(L"%ls: No binding found for key '%ls'\n"),
argv[0], eseq.c_str());
} else {
seq = argv[w.woptind];
streams.err.append_format(_(L"%ls: No binding found for sequence '%ls'\n"),
argv[0], eseq.c_str());
}
if (!builtin_bind_list_one(seq, bind_mode, streams)) {
res = STATUS_BUILTIN_ERROR;
wcstring eseq = escape_string(argv[w.woptind], 0);
if (use_terminfo) {
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());
}
}
break;
}
default: {
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;
} 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;
@ -1776,39 +1763,33 @@ static int builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **arg
}
}
switch (argc - w.woptind) {
case 0: {
long res;
if (!seeded) {
seeded = 1;
srand48_r(time(0), &seed_buffer);
}
lrand48_r(&seed_buffer, &res);
streams.out.append_format(L"%ld\n", res % 32768);
break;
}
case 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]);
return STATUS_BUILTIN_ERROR;
}
int arg_count = argc - w.woptind;
if (arg_count == 0) {
long res;
if (!seeded) {
seeded = 1;
srand48_r(foo, &seed_buffer);
break;
srand48_r(time(0), &seed_buffer);
}
default: {
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);
lrand48_r(&seed_buffer, &res);
streams.out.append_format(L"%ld\n", res % 32768);
} 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]);
return STATUS_BUILTIN_ERROR;
}
seeded = 1;
srand48_r(foo, &seed_buffer);
} 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,19 +1881,16 @@ 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: {
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);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
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;
} 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;
@ -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: {
memset(&state, 0, sizeof(state));
break;
}
case (size_t)-2: {
break;
}
default: {
finished = 1;
break;
}
if (sz == (size_t)-1) {
memset(&state, 0, sizeof(state));
} else if (sz != (size_t)-2) {
finished = 1;
}
}
}
@ -2310,27 +2280,22 @@ 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;
}
case 2: {
wchar_t *end;
errno = 0;
ec = wcstol(argv[1], &end, 10);
if (errno || *end != 0) {
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0],
argv[1]);
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]);
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;
}
long ec;
if (argc == 1) {
ec = proc_get_last_status();
} else {
wchar_t *end;
errno = 0;
ec = wcstol(argv[1], &end, 10);
if (errno || *end != 0) {
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0],
argv[1]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
@ -2752,29 +2717,26 @@ 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;
}
case 2: {
wchar_t *end;
errno = 0;
status = fish_wcstoi(argv[1], &end, 10);
if (errno || *end != 0) {
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0],
argv[1]);
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]);
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;
}
int status;
if (argc == 2) {
wchar_t *end;
errno = 0;
status = fish_wcstoi(argv[1], &end, 10);
if (errno || *end != 0) {
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0],
argv[1]);
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: {
wcstring tmp = token.text;
unescape_string_in_place(&tmp, UNESCAPE_INCOMPLETE);
out.append(tmp);
out.push_back(L'\n');
break;
}
default: { break; }
if (token.type == TOK_STRING) {
wcstring tmp = token.text;
unescape_string_in_place(&tmp, UNESCAPE_INCOMPLETE);
out.append(tmp);
out.push_back(L'\n');
}
}
@ -476,24 +472,18 @@ int builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv)
}
}
switch (argc - w.woptind) {
case 0: {
write_part(begin, end, cut_at_cursor, tokenize, streams);
break;
}
case 1: {
replace_part(begin, end, argv[w.woptind], append_mode);
break;
}
default: {
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;
int arg_count = argc - w.woptind;
if (arg_count == 0) {
write_part(begin, end, cut_at_cursor, tokenize, streams);
} else if (arg_count == 1) {
replace_part(begin, end, argv[w.woptind], append_mode);
} 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);
}
return 0;

View file

@ -579,62 +579,55 @@ bool binary_primary::evaluate(wcstring_list_t &errors) {
}
bool unary_operator::evaluate(wcstring_list_t &errors) {
switch (token) {
case test_bang: {
assert(subject.get());
return !subject->evaluate(errors);
}
default: {
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return false;
}
if (token == test_bang) {
assert(subject.get());
return !subject->evaluate(errors);
}
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: {
assert(!subjects.empty());
assert(combiners.size() + 1 == subjects.size());
if (token == test_combine_and || token == test_combine_or) {
assert(!subjects.empty());
assert(combiners.size() + 1 == subjects.size());
// One-element case.
if (subjects.size() == 1) return subjects.at(0)->evaluate(errors);
// One-element case.
if (subjects.size() == 1) return subjects.at(0)->evaluate(errors);
// Evaluate our lists, remembering that AND has higher precedence than OR. We can
// visualize this as a sequence of OR expressions of AND expressions.
size_t idx = 0, max = subjects.size();
bool or_result = false;
while (idx < max) {
if (or_result) { // short circuit
// Evaluate our lists, remembering that AND has higher precedence than OR. We can
// visualize this as a sequence of OR expressions of AND expressions.
size_t idx = 0, max = subjects.size();
bool or_result = false;
while (idx < max) {
if (or_result) { // short circuit
break;
}
// Evaluate a stream of AND starting at given subject index. It may only have one
// element.
bool and_result = true;
for (; idx < max; idx++) {
// Evaluate it, short-circuiting.
and_result = and_result && subjects.at(idx)->evaluate(errors);
// If the combiner at this index (which corresponding to how we combine with the
// next subject) is not AND, then exit the loop.
if (idx + 1 < max && combiners.at(idx) != test_combine_and) {
idx++;
break;
}
// Evaluate a stream of AND starting at given subject index. It may only have one
// element.
bool and_result = true;
for (; idx < max; idx++) {
// Evaluate it, short-circuiting.
and_result = and_result && subjects.at(idx)->evaluate(errors);
// If the combiner at this index (which corresponding to how we combine with the
// next subject) is not AND, then exit the loop.
if (idx + 1 < max && combiners.at(idx) != test_combine_and) {
idx++;
break;
}
}
// OR it in.
or_result = or_result || and_result;
}
return or_result;
}
default: {
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return BUILTIN_TEST_FAIL;
// OR it in.
or_result = or_result || and_result;
}
return or_result;
}
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return BUILTIN_TEST_FAIL;
}
bool parenthetical_expression::evaluate(wcstring_list_t &errors) {
@ -798,42 +791,36 @@ 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: {
// 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);
if (!expr) {
#if 0
printf("Oops! test was given args:\n");
for (size_t i=0; i < argc; i++) {
printf("\t%ls\n", args.at(i).c_str());
}
printf("and returned parse error: %ls\n", err.c_str());
#endif
streams.err.append(err);
return BUILTIN_TEST_FAIL;
}
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;
}
wcstring_list_t eval_errors;
bool result = expr->evaluate(eval_errors);
if (!eval_errors.empty()) {
printf("test returned eval errors:\n");
for (size_t i = 0; i < eval_errors.size(); i++) {
printf("\t%ls\n", eval_errors.at(i).c_str());
}
}
delete expr;
return result ? BUILTIN_TEST_SUCCESS : BUILTIN_TEST_FAIL;
// Try parsing. If expr is not nil, we are responsible for deleting it.
wcstring err;
expression *expr = test_parser::parse_args(args, err);
if (!expr) {
#if 0
printf("Oops! test was given args:\n");
for (size_t i=0; i < argc; i++) {
printf("\t%ls\n", args.at(i).c_str());
}
printf("and returned parse error: %ls\n", err.c_str());
#endif
streams.err.append(err);
return BUILTIN_TEST_FAIL;
}
wcstring_list_t eval_errors;
bool result = expr->evaluate(eval_errors);
if (!eval_errors.empty()) {
printf("test returned eval errors:\n");
for (size_t i = 0; i < eval_errors.size(); i++) {
printf("\t%ls\n", eval_errors.at(i).c_str());
}
}
return 1;
delete expr;
return result ? BUILTIN_TEST_SUCCESS : BUILTIN_TEST_FAIL;
}

View file

@ -278,46 +278,42 @@ 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
print(what, hard, streams);
break;
}
case 1: { // change current limit value
rlim_t new_limit;
wchar_t *end;
int arg_count = argc - w.woptind;
if (arg_count == 0) {
// Show current limit value.
print(what, hard, streams);
} else if (arg_count == 1) {
// Change current limit value.
rlim_t new_limit;
wchar_t *end;
// Set both hard and soft limits if nothing else was specified.
if (!(hard + soft)) {
hard = soft = 1;
// Set both hard and soft limits if nothing else was specified.
if (!(hard + soft)) {
hard = soft = 1;
}
if (wcscasecmp(argv[w.woptind], L"unlimited") == 0) {
new_limit = RLIM_INFINITY;
} else if (wcscasecmp(argv[w.woptind], L"hard") == 0) {
new_limit = get(what, 1);
} else if (wcscasecmp(argv[w.woptind], L"soft") == 0) {
new_limit = get(what, soft);
} else {
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]);
builtin_print_help(parser, streams, argv[0], streams.err);
return 1;
}
if (wcscasecmp(argv[w.woptind], L"unlimited") == 0) {
new_limit = RLIM_INFINITY;
} else if (wcscasecmp(argv[w.woptind], L"hard") == 0) {
new_limit = get(what, 1);
} else if (wcscasecmp(argv[w.woptind], L"soft") == 0) {
new_limit = get(what, soft);
} else {
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]);
builtin_print_help(parser, streams, argv[0], streams.err);
return 1;
}
new_limit *= get_multiplier(what);
}
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;
new_limit *= get_multiplier(what);
}
return set(what, hard, soft, new_limit, streams);
}
return 0;
streams.err.append(argv[0]);
streams.err.append(L": Too many arguments\n");
builtin_print_help(parser, streams, argv[0], streams.err);
return 1;
}

View file

@ -166,36 +166,31 @@ wcstring expand_escape_variable(const wcstring &in) {
tokenize_variable_array(in, lst);
switch (lst.size()) {
case 0: {
buff.append(L"''");
break;
}
case 1: {
const wcstring &el = lst.at(0);
int size = lst.size();
if (size == 0) {
buff.append(L"''");
} else if (size == 1) {
const wcstring &el = lst.at(0);
if (el.find(L' ') != wcstring::npos && is_quotable(el)) {
if (el.find(L' ') != wcstring::npos && is_quotable(el)) {
buff.append(L"'");
buff.append(el);
buff.append(L"'");
} else {
buff.append(escape_string(el, 1));
}
} else {
for (size_t j = 0; j < lst.size(); j++) {
const wcstring &el = lst.at(j);
if (j) buff.append(L" ");
if (is_quotable(el)) {
buff.append(L"'");
buff.append(el);
buff.append(L"'");
} else {
buff.append(escape_string(el, 1));
}
break;
}
default: {
for (size_t j = 0; j < lst.size(); j++) {
const wcstring &el = lst.at(j);
if (j) buff.append(L" ");
if (is_quotable(el)) {
buff.append(L"'");
buff.append(el);
buff.append(L"'");
} else {
buff.append(escape_string(el, 1));
}
}
}
}
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,26 +110,17 @@ 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 (interrupt_handler) {
int res = interrupt_handler();
if (res) {
return res;
}
if (has_lookahead()) {
return lookahead_pop();
}
}
if (errno == EINTR || errno == EAGAIN) {
if (interrupt_handler) {
int res = interrupt_handler();
if (res) return res;
if (has_lookahead()) return lookahead_pop();
}
do_loop = true;
break;
}
default: {
// The terminal has been closed. Save and exit.
return R_EOF;
}
do_loop = true;
} else {
// The terminal has been closed. Save and exit.
return R_EOF;
}
} else {
// Assume we loop unless we see a character in stdin.

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: {
iothread_service_main_thread_requests();
break;
}
case IO_SERVICE_RESULT_QUEUE: {
iothread_service_result_queue();
break;
}
default: {
fprintf(stderr, "Unknown wakeup byte %02x in %s\n", wakeup_byte, __FUNCTION__);
break;
}
if (wakeup_byte == IO_SERVICE_MAIN_THREAD_REQUEST_QUEUE) {
iothread_service_main_thread_requests();
} else if (wakeup_byte == IO_SERVICE_RESULT_QUEUE) {
iothread_service_result_queue();
} else {
fprintf(stderr, "Unknown wakeup byte %02x in %s\n", wakeup_byte, __FUNCTION__);
}
}

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: {
// 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: {
// For other types, we only require that the types match.
matched = true;
break;
}
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);
} else {
// For other types, we only require that the types match.
matched = true;
}
}
@ -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,28 +1019,23 @@ 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: {
wchar_t next_char = idx + 1 < unesc_size ? unesc.at(idx + 1) : L'\0';
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 &&
!wcsvarchr(next_char)) {
err = 1;
if (out_errors) {
// We have something like $$$^.... Back up until we reach the first $.
size_t first_dollar = idx;
while (first_dollar > 0 &&
(unesc.at(first_dollar - 1) == VARIABLE_EXPAND ||
unesc.at(first_dollar - 1) == VARIABLE_EXPAND_SINGLE)) {
first_dollar--;
}
parse_util_expand_variable_error(unesc, node.source_start, first_dollar,
out_errors);
if (next_char != VARIABLE_EXPAND && next_char != VARIABLE_EXPAND_SINGLE &&
!wcsvarchr(next_char)) {
err = 1;
if (out_errors) {
// We have something like $$$^.... Back up until we reach the first $.
size_t first_dollar = idx;
while (first_dollar > 0 &&
(unesc.at(first_dollar - 1) == VARIABLE_EXPAND ||
unesc.at(first_dollar - 1) == VARIABLE_EXPAND_SINGLE)) {
first_dollar--;
}
parse_util_expand_variable_error(unesc, node.source_start, first_dollar,
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: {
prepend_wd = false;
break;
}
default: {
prepend_wd = true;
break;
}
if (path.at(0) == L'/' || path.at(0) == HOME_DIRECTORY) {
prepend_wd = false;
} else {
prepend_wd = true;
}
if (!prepend_wd) {

View file

@ -1379,27 +1379,23 @@ static bool handle_completions(const std::vector<completion_t> &comp,
const wcstring tok(begin, end - begin);
// Check trivial cases.
switch (comp.size()) {
case 0: {
// No suitable completions found, flash screen and return.
reader_flash();
done = true;
success = false;
break;
}
case 1: {
// Exactly one suitable completion found - insert it.
const completion_t &c = comp.at(0);
int size = comp.size();
if (size == 0) {
// No suitable completions found, flash screen and return.
reader_flash();
done = true;
success = false;
} else if (size == 1) {
// Exactly one suitable completion found - insert it.
const completion_t &c = comp.at(0);
// If this is a replacement completion, check that we know how to replace it, e.g. that
// the token doesn't contain evil operators like {}.
if (!(c.flags & COMPLETE_REPLACES_TOKEN) || reader_can_replace(tok, c.flags)) {
completion_insert(c.completion.c_str(), c.flags);
}
done = true;
success = true;
break;
// If this is a replacement completion, check that we know how to replace it, e.g. that
// the token doesn't contain evil operators like {}.
if (!(c.flags & COMPLETE_REPLACES_TOKEN) || reader_can_replace(tok, c.flags)) {
completion_insert(c.completion.c_str(), c.flags);
}
done = true;
success = true;
}
if (!done) {
@ -1790,24 +1786,22 @@ 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.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) {
break;
}
// debug( 3, L"ok pos" );
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;
}
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) {
break;
}
// debug( 3, L"ok pos" );
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;
}
break;
}
default: { break; }
} else {
break;
}
}
}
@ -2873,36 +2867,30 @@ const wchar_t *reader_readline(int nchars) {
data->history_search.skip_matches(skip_list);
}
switch (data->search_mode) {
case LINE_SEARCH: {
if ((c == R_HISTORY_SEARCH_BACKWARD) ||
(c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) {
data->history_search.go_backwards();
} else {
if (!data->history_search.go_forwards()) {
// If you try to go forwards past the end, we just go to the end.
data->history_search.go_to_end();
}
if (data->search_mode == LINE_SEARCH) {
if ((c == R_HISTORY_SEARCH_BACKWARD) ||
(c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) {
data->history_search.go_backwards();
} else {
if (!data->history_search.go_forwards()) {
// If you try to go forwards past the end, we just go to the end.
data->history_search.go_to_end();
}
wcstring new_text;
if (data->history_search.is_at_end()) {
new_text = data->search_buff;
} 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: {
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;
wcstring new_text;
if (data->history_search.is_at_end()) {
new_text = data->search_buff;
} else {
new_text = data->history_search.current_string();
}
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;

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: {
// On unknown errors we do nothing. The file will be given the default 'File'
// description or one based on the suffix.
}
}
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)) {