lint: Use early exit/continue

This commit is contained in:
Kurtis Rader 2016-10-30 19:17:08 -07:00
parent 3bd24ddb17
commit 46b791240a

View file

@ -188,10 +188,12 @@ void builtin_print_help(parser_t &parser, io_streams_t &streams, const wchar_t *
if (pos && *pos) {
// Then find the next empty line.
for (; *pos; pos++) {
if (*pos == L'\n') {
wchar_t *pos2;
int is_empty = 1;
if (*pos != L'\n') {
continue;
}
int is_empty = 1;
wchar_t *pos2;
for (pos2 = pos + 1; *pos2; pos2++) {
if (*pos2 == L'\n') break;
@ -208,7 +210,6 @@ void builtin_print_help(parser_t &parser, io_streams_t &streams, const wchar_t *
}
}
}
}
// We did not find a good place to cut message to shorten it - so we make sure we
// don't print anything.
@ -1198,71 +1199,61 @@ static int builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **
return res;
}
// Convert a octal or hex character to its binary value. Surprisingly a version
// of this function using a lookup table is only ~1.5% faster than the `switch`
// statement version below. Since that requires initializing a table statically
// (which is problematic if we run on an EBCDIC system) we don't use that
// solution. Also, we relax the style rule that `case` blocks should always be
// enclosed in parentheses given the nature of this code.
static unsigned int builtin_echo_digit(wchar_t wc, unsigned int base) {
assert(base == 8 || base == 16); // base must be hex or octal
switch (wc) {
case L'0': {
case L'0':
return 0;
}
case L'1': {
case L'1':
return 1;
}
case L'2': {
case L'2':
return 2;
}
case L'3': {
case L'3':
return 3;
}
case L'4': {
case L'4':
return 4;
}
case L'5': {
case L'5':
return 5;
}
case L'6': {
case L'6':
return 6;
}
case L'7': {
case L'7':
return 7;
}
default: { break; }
}
if (base == 16) {
if (base != 16) return UINT_MAX;
switch (wc) {
case L'8': {
case L'8':
return 8;
}
case L'9': {
case L'9':
return 9;
}
case L'a':
case L'A': {
case L'A':
return 10;
}
case L'b':
case L'B': {
case L'B':
return 11;
}
case L'c':
case L'C': {
case L'C':
return 12;
}
case L'd':
case L'D': {
case L'D':
return 13;
}
case L'e':
case L'E': {
case L'E':
return 14;
}
case L'f':
case L'F': {
case L'F':
return 15;
}
default: { break; }
}
}
return UINT_MAX;
}
@ -1295,7 +1286,10 @@ static bool builtin_echo_parse_numeric_sequence(const wchar_t *str, size_t *cons
start = 1;
}
if (base != 0) {
if (base == 0) {
return success;
}
unsigned int idx;
unsigned char val = 0; // resulting character
for (idx = start; idx < start + max_digits; idx++) {
@ -1310,7 +1304,6 @@ static bool builtin_echo_parse_numeric_sequence(const wchar_t *str, size_t *cons
*out_val = val;
success = true;
}
}
return success;
}
@ -1546,7 +1539,7 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis
{L"inherit-variable", required_argument, 0, 'V'},
{0, 0, 0, 0}};
while (1 && !res) {
while (res == STATUS_BUILTIN_OK) {
int opt_index = 0;
// The leading - here specifies RETURN_IN_ORDER.
@ -1556,7 +1549,7 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis
case 0: {
if (long_options[opt_index].flag != 0) break;
append_format(*out_err, BUILTIN_ERR_UNKNOWN, argv[0], long_options[opt_index].name);
res = 1;
res = STATUS_BUILTIN_ERROR;
break;
}
case 'd': {
@ -1567,7 +1560,7 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis
int sig = wcs2sig(w.woptarg);
if (sig < 0) {
append_format(*out_err, _(L"%ls: Unknown signal '%ls'"), argv[0], w.woptarg);
res = 1;
res = STATUS_BUILTIN_ERROR;
break;
}
events.push_back(event_t::signal_event(sig));
@ -1617,7 +1610,7 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis
append_format(*out_err,
_(L"%ls: Cannot find calling job for event handler"),
argv[0]);
res = 1;
res = STATUS_BUILTIN_ERROR;
} else {
e.type = EVENT_JOB_ID;
e.param1.job_id = job_id;
@ -1628,14 +1621,14 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis
if (errno || !end || *end) {
append_format(*out_err, _(L"%ls: Invalid process id %ls"), argv[0],
w.woptarg);
res = 1;
res = STATUS_BUILTIN_ERROR;
break;
}
e.type = EVENT_EXIT;
e.param1.pid = (opt == 'j' ? -1 : 1) * abs(pid);
}
if (!res) {
if (res == STATUS_BUILTIN_OK) {
events.push_back(e);
}
break;
@ -1676,7 +1669,7 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis
}
case '?': {
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
res = 1;
res = STATUS_BUILTIN_ERROR;
break;
}
default: {
@ -1686,7 +1679,10 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis
}
}
if (!res) {
if (res != STATUS_BUILTIN_OK) {
return STATUS_BUILTIN_ERROR;
}
// Determine the function name, and remove it from the list of positionals.
wcstring function_name;
bool name_is_missing = positionals.empty();
@ -1702,22 +1698,22 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis
if (name_is_missing) {
append_format(*out_err, _(L"%ls: Expected function name"), argv[0]);
res = 1;
res = STATUS_BUILTIN_ERROR;
} else if (wcsfuncname(function_name)) {
append_format(*out_err, _(L"%ls: Illegal function name '%ls'"), argv[0],
function_name.c_str());
res = 1;
res = STATUS_BUILTIN_ERROR;
} else if (parser_keywords_is_reserved(function_name)) {
append_format(
*out_err,
_(L"%ls: The name '%ls' is reserved,\nand can not be used as a function name"),
argv[0], function_name.c_str());
_(L"%ls: The name '%ls' is reserved,\nand can not be used as a function name"), argv[0],
function_name.c_str());
res = 1;
res = STATUS_BUILTIN_ERROR;
} else if (function_name.empty()) {
append_format(*out_err, _(L"%ls: No function name given"), argv[0]);
res = 1;
res = STATUS_BUILTIN_ERROR;
} else {
if (has_named_arguments) {
// All remaining positionals are named arguments.
@ -1734,11 +1730,14 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis
// +1 because we already got the function name.
append_format(*out_err, _(L"%ls: Expected one argument, got %lu"), argv[0],
(unsigned long)(positionals.size() + 1));
res = 1;
res = STATUS_BUILTIN_ERROR;
}
}
if (!res) {
if (res != STATUS_BUILTIN_OK) {
return res;
}
// Here we actually define the function!
function_data_t d;
@ -1755,15 +1754,12 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis
}
d.definition = contents.c_str();
function_add(d, parser, definition_line_offset);
// Handle wrap targets.
for (size_t w = 0; w < wrap_targets.size(); w++) {
complete_add_wrapper(function_name, wrap_targets.at(w));
}
}
}
return res;
}
@ -2098,7 +2094,10 @@ static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv)
}
}
if (i != argc && !exit_res) {
if (i == argc || exit_res != STATUS_BUILTIN_OK) {
return exit_res;
}
env_var_t ifs = env_get_string(L"IFS");
if (ifs.missing_or_empty()) {
// Every character is a separate token.
@ -2145,12 +2144,10 @@ static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv)
while (i < argc) {
loc = wcstring_tok(buff, (i + 1 < argc) ? ifs : wcstring(), loc);
env_set(argv[i], loc.first == wcstring::npos ? L"" : &buff.c_str()[loc.first],
place);
env_set(argv[i], loc.first == wcstring::npos ? L"" : &buff.c_str()[loc.first], place);
++i;
}
}
}
return exit_res;
}
@ -2244,7 +2241,7 @@ static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **arg
else {
streams.err.append_format(L"%ls: Invalid job control mode '%ls'\n", L"status",
w.woptarg);
res = 1;
res = STATUS_BUILTIN_ERROR;
}
mode = DONE;
break;
@ -2268,23 +2265,24 @@ static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **arg
}
}
if (!res) {
if (res == STATUS_BUILTIN_ERROR) {
return res;
}
switch (mode) {
case DONE: {
break;
return STATUS_BUILTIN_OK;
}
case CURRENT_FILENAME: {
const wchar_t *fn = parser.current_filename();
if (!fn) fn = _(L"Standard input");
streams.out.append_format(L"%ls\n", fn);
break;
return STATUS_BUILTIN_OK;
}
case CURRENT_LINE_NUMBER: {
streams.out.append_format(L"%d\n", parser.get_lineno());
break;
return STATUS_BUILTIN_OK;
}
case IS_INTERACTIVE: {
return !is_interactive_session;
@ -2309,13 +2307,14 @@ static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **arg
}
case STACK_TRACE: {
streams.out.append(parser.stack_trace());
break;
return STATUS_BUILTIN_OK;
}
case NORMAL: {
if (is_login)
if (is_login) {
streams.out.append_format(_(L"This is a login shell\n"));
else
} else {
streams.out.append_format(_(L"This is not a login shell\n"));
}
streams.out.append_format(
_(L"Job control: %ls\n"),
@ -2323,13 +2322,12 @@ static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **arg
? _(L"Only on interactive jobs")
: (job_control_mode == JOB_CONTROL_NONE ? _(L"Never") : _(L"Always")));
streams.out.append(parser.stack_trace());
break;
return STATUS_BUILTIN_OK;
}
default: { break; }
}
}
return res;
DIE("status subcommand not handled");
}
/// The exit builtin. Calls reader_exit to exit and returns the value specified.
@ -2637,7 +2635,10 @@ static int builtin_fg(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
}
}
if (j) {
if (!j) {
return STATUS_BUILTIN_ERROR;
}
if (streams.err_is_redirected) {
streams.err.append_format(FG_MSG, j->job_id, j->command_wcstr());
} else {
@ -2654,8 +2655,7 @@ static int builtin_fg(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
job_set_flag(j, JOB_FOREGROUND, 1);
job_continue(j, job_is_stopped(j));
}
return j ? STATUS_BUILTIN_OK : STATUS_BUILTIN_ERROR;
return STATUS_BUILTIN_OK;
}
/// Helper function for builtin_bg().