cocci: use strempty() at more places

This shortens the code by a few lines.
This commit is contained in:
Lennart Poettering 2018-01-10 17:11:19 +01:00
parent 4631116dd9
commit ad5d4b1703
8 changed files with 49 additions and 19 deletions

View File

@ -8,3 +8,41 @@ expression s;
@@
- s ? s : ""
+ strempty(s)
@@
expression s;
@@
- if (!s)
- s = "";
+ s = strempty(s);
@@
expression s;
@@
- s ?: "(null)"
+ strnull(s)
@@
expression s;
@@
- s ? s : "(null)"
+ strnull(s)
@@
expression s;
@@
- if (!s)
- s = "(null)";
+ s = strnull(s);
@@
expression s;
@@
- s ?: "n/a"
+ strna(s)
@@
expression s;
@@
- s ? s : "n/a"
+ strna(s)
@@
expression s;
@@
- if (!s)
- s = "n/a";
+ s = strna(s);

View File

@ -1225,8 +1225,7 @@ int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
if (!filename_is_valid(fn))
return -EINVAL;
if (!extra)
extra = "";
extra = strempty(extra);
t = new(char, strlen(p) + 2 + strlen(extra) + 6 + 1);
if (!t)
@ -1259,8 +1258,7 @@ int tempfn_random(const char *p, const char *extra, char **ret) {
if (!filename_is_valid(fn))
return -EINVAL;
if (!extra)
extra = "";
extra = strempty(extra);
t = new(char, strlen(p) + 2 + strlen(extra) + 16 + 1);
if (!t)
@ -1300,8 +1298,7 @@ int tempfn_random_child(const char *p, const char *extra, char **ret) {
return r;
}
if (!extra)
extra = "";
extra = strempty(extra);
t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1);
if (!t)

View File

@ -52,15 +52,15 @@ static inline bool streq_ptr(const char *a, const char *b) {
}
static inline const char* strempty(const char *s) {
return s ? s : "";
return s ?: "";
}
static inline const char* strnull(const char *s) {
return s ? s : "(null)";
return s ?: "(null)";
}
static inline const char *strna(const char *s) {
return s ? s : "n/a";
return s ?: "n/a";
}
static inline bool isempty(const char *p) {

View File

@ -306,8 +306,7 @@ void job_dump(Job *j, FILE*f, const char *prefix) {
assert(j);
assert(f);
if (!prefix)
prefix = "";
prefix = strempty(prefix);
fprintf(f,
"%s-> Job %u:\n"

View File

@ -34,8 +34,7 @@ void kill_context_init(KillContext *c) {
void kill_context_dump(KillContext *c, FILE *f, const char *prefix) {
assert(c);
if (!prefix)
prefix = "";
prefix = strempty(prefix);
fprintf(f,
"%sKillMode: %s\n"

View File

@ -418,8 +418,7 @@ _public_ int sd_journal_stream_fd(const char *identifier, int priority, int leve
fd_inc_sndbuf(fd, SNDBUF_SIZE);
if (!identifier)
identifier = "";
identifier = strempty(identifier);
l = strlen(identifier);
header = alloca(l + 1 + 1 + 2 + 2 + 2 + 2 + 2);

View File

@ -653,8 +653,7 @@ void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_
assert(c);
assert(f);
if (!prefix)
prefix = "";
prefix = strempty(prefix);
fprintf(f,
"%s\t%s: %s%s%s %s\n",

View File

@ -102,8 +102,7 @@ static void get_cap_mask(struct udev_device *dev,
unsigned long val;
v = udev_device_get_sysattr_value(pdev, attr);
if (!v)
v = "";
v = strempty(v);
xsprintf(text, "%s", v);
log_debug("%s raw kernel attribute: %s", attr, text);