]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/hashmap: tweak code to avoid pointless gcc warning
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 2 Feb 2018 13:34:00 +0000 (14:34 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 2 Feb 2018 13:34:00 +0000 (14:34 +0100)
gcc says:
[196/1142] Compiling C object 'src/basic/basic@sta/hashmap.c.o'.
../src/basic/hashmap.c: In function ‘cachemem_maintain’:
../src/basic/hashmap.c:1913:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
                 mem->active = r = true;
                 ^~~

which conflates two things: the first is transitive assignent a = b = c = d;
the second is assignment of the value of an expression, which happens to be a
an assignment expression here, and boolean. While the second _should_ be
parenthesized, the first should _not_, and it's more natural to understand
our code as the first, and gcc should treat this as an exception and not emit
the warning. But since it's a while until this will be fixed, let's update
our code too.

src/basic/hashmap.c

index 32be96455de1e2983a358ab4e4a239c4459d1126..d0873f7ae517e7a181bfafcd78b2cf937637ff92 100644 (file)
@@ -1900,8 +1900,6 @@ int set_put_strsplit(Set *s, const char *v, const char *separators, ExtractFlags
 
 /* expand the cachemem if needed, return true if newly (re)activated. */
 static int cachemem_maintain(CacheMem *mem, unsigned size) {
-        int r = false;
-
         assert(mem);
 
         if (!GREEDY_REALLOC(mem->ptr, mem->n_allocated, size)) {
@@ -1909,10 +1907,12 @@ static int cachemem_maintain(CacheMem *mem, unsigned size) {
                         return -ENOMEM;
         }
 
-        if (!mem->active)
-                mem->active = r = true;
+        if (!mem->active) {
+                mem->active = true;
+                return true;
+        }
 
-        return r;
+        return false;
 }
 
 int iterated_cache_get(IteratedCache *cache, const void ***res_keys, const void ***res_values, unsigned *res_n_entries) {