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 : "" - s ? s : ""
+ strempty(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)) if (!filename_is_valid(fn))
return -EINVAL; return -EINVAL;
if (!extra) extra = strempty(extra);
extra = "";
t = new(char, strlen(p) + 2 + strlen(extra) + 6 + 1); t = new(char, strlen(p) + 2 + strlen(extra) + 6 + 1);
if (!t) if (!t)
@ -1259,8 +1258,7 @@ int tempfn_random(const char *p, const char *extra, char **ret) {
if (!filename_is_valid(fn)) if (!filename_is_valid(fn))
return -EINVAL; return -EINVAL;
if (!extra) extra = strempty(extra);
extra = "";
t = new(char, strlen(p) + 2 + strlen(extra) + 16 + 1); t = new(char, strlen(p) + 2 + strlen(extra) + 16 + 1);
if (!t) if (!t)
@ -1300,8 +1298,7 @@ int tempfn_random_child(const char *p, const char *extra, char **ret) {
return r; return r;
} }
if (!extra) extra = strempty(extra);
extra = "";
t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1); t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1);
if (!t) 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) { static inline const char* strempty(const char *s) {
return s ? s : ""; return s ?: "";
} }
static inline const char* strnull(const char *s) { static inline const char* strnull(const char *s) {
return s ? s : "(null)"; return s ?: "(null)";
} }
static inline const char *strna(const char *s) { static inline const char *strna(const char *s) {
return s ? s : "n/a"; return s ?: "n/a";
} }
static inline bool isempty(const char *p) { 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(j);
assert(f); assert(f);
if (!prefix) prefix = strempty(prefix);
prefix = "";
fprintf(f, fprintf(f,
"%s-> Job %u:\n" "%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) { void kill_context_dump(KillContext *c, FILE *f, const char *prefix) {
assert(c); assert(c);
if (!prefix) prefix = strempty(prefix);
prefix = "";
fprintf(f, fprintf(f,
"%sKillMode: %s\n" "%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); fd_inc_sndbuf(fd, SNDBUF_SIZE);
if (!identifier) identifier = strempty(identifier);
identifier = "";
l = strlen(identifier); l = strlen(identifier);
header = alloca(l + 1 + 1 + 2 + 2 + 2 + 2 + 2); 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(c);
assert(f); assert(f);
if (!prefix) prefix = strempty(prefix);
prefix = "";
fprintf(f, fprintf(f,
"%s\t%s: %s%s%s %s\n", "%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; unsigned long val;
v = udev_device_get_sysattr_value(pdev, attr); v = udev_device_get_sysattr_value(pdev, attr);
if (!v) v = strempty(v);
v = "";
xsprintf(text, "%s", v); xsprintf(text, "%s", v);
log_debug("%s raw kernel attribute: %s", attr, text); log_debug("%s raw kernel attribute: %s", attr, text);