Skip to contentSkip to conversation

block/snapshot: Fix compiler warning with -Wshadow=local

PATCH In review Thomas Huth 3 participants 5 messages Oct 23, 2023 – Nov 7, 2023 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 block/snapshot: Fix compiler warning with -Wshadow=local

No need to declare a new variable in the the inner code block here, we can re-use the "ret" variable that has been declared at the beginning of the function. With this change, the code can now be successfully compiled with -Wshadow=local again.

Fixes a32e781838 ("Mark bdrv_snapshot_fallback() and callers GRAPH_RDLOCK")

---
v2: Assign "ret" only in one spot

 block/snapshot.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
block/snapshot.c +2 -4
index 6e16eb803a..55974273ae 100644
@@ -629,7 +629,6 @@ int bdrv_all_goto_snapshot(const char *name,
     while (iterbdrvs) {
         BlockDriverState *bs = iterbdrvs->data;
         AioContext *ctx = bdrv_get_aio_context(bs);
-        int ret = 0;
         bool all_snapshots_includes_bs;
 
         aio_context_acquire(ctx);
@@ -637,9 +636,8 @@ int bdrv_all_goto_snapshot(const char *name,
         all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs);
         bdrv_graph_rdunlock_main_loop();
 
-        if (devices || all_snapshots_includes_bs) {
-            ret = bdrv_snapshot_goto(bs, name, errp);
-        }
+        ret = (devices || all_snapshots_includes_bs) ?
+              bdrv_snapshot_goto(bs, name, errp) : 0;
         aio_context_release(ctx);
         if (ret < 0) {
             bdrv_graph_rdlock_main_loop();
Signature · 1 line
2.41.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 27 lines · Thomas Huth

No need to declare a new variable in the the inner code block here, we can re-use the "ret" variable that has been declared at the beginning of the function. With this change, the code can now be successfully compiled with -Wshadow=local again.

Fixes a32e781838 ("Mark bdrv_snapshot_fallback() and callers GRAPH_RDLOCK")

---
v2: Assign "ret" only in one spot

 block/snapshot.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
block/snapshot.c +0 -1
index 6e16eb803a..55974273ae 100644
@@ -629,7 +629,6 @@ int bdrv_all_goto_snapshot(const char *name,
     while (iterbdrvs) {
         BlockDriverState *bs = iterbdrvs->data;
         AioContext *ctx = bdrv_get_aio_context(bs);
-        int ret = 0;
         bool all_snapshots_includes_bs;
 
         aio_context_acquire(ctx);
@@ -637,9 +636,8 @@ int bdrv_all_goto_snapshot(const char *name,
         all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs);

block/snapshot.c int bdrv_all_goto_snapshot(const char *name, · line 643

Quoted 10 lines
block/snapshot.c +2 -3
         bdrv_graph_rdunlock_main_loop();
 
-        if (devices || all_snapshots_includes_bs) {
-            ret = bdrv_snapshot_goto(bs, name, errp);
-        }
+        ret = (devices || all_snapshots_includes_bs) ?
+              bdrv_snapshot_goto(bs, name, errp) : 0;
         aio_context_release(ctx);
         if (ret < 0) {
             bdrv_graph_rdlock_main_loop();

Better. Unconditional assignment to @ret right before it's checked is how we should use @ret.

And queued. Thanks!

#2 lore raw
Quoted 43 lines · Markus Armbruster
Quoted 27 lines · Thomas Huth

No need to declare a new variable in the the inner code block here, we can re-use the "ret" variable that has been declared at the beginning of the function. With this change, the code can now be successfully compiled with -Wshadow=local again.

Fixes a32e781838 ("Mark bdrv_snapshot_fallback() and callers GRAPH_RDLOCK")

---
v2: Assign "ret" only in one spot

 block/snapshot.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
block/snapshot.c +0 -1
index 6e16eb803a..55974273ae 100644
@@ -629,7 +629,6 @@ int bdrv_all_goto_snapshot(const char *name,
     while (iterbdrvs) {
         BlockDriverState *bs = iterbdrvs->data;
         AioContext *ctx = bdrv_get_aio_context(bs);
-        int ret = 0;
         bool all_snapshots_includes_bs;
 
         aio_context_acquire(ctx);
@@ -637,9 +636,8 @@ int bdrv_all_goto_snapshot(const char *name,
         all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs);

block/snapshot.c int bdrv_all_goto_snapshot(const char *name, · line 643

Quoted 10 lines
block/snapshot.c +2 -3
         bdrv_graph_rdunlock_main_loop();
 
-        if (devices || all_snapshots_includes_bs) {
-            ret = bdrv_snapshot_goto(bs, name, errp);
-        }
+        ret = (devices || all_snapshots_includes_bs) ?
+              bdrv_snapshot_goto(bs, name, errp) : 0;
         aio_context_release(ctx);
         if (ret < 0) {
             bdrv_graph_rdlock_main_loop();

Better. Unconditional assignment to @ret right before it's checked is how we should use @ret.

Quoted 3 lines

And queued. Thanks!

Mind if I drop the Fixes: tag? Nothing broken until we enable -Wshadow=local...

#3 lore raw
Quoted 45 lines · Markus Armbruster
Quoted 43 lines · Markus Armbruster
Quoted 27 lines · Thomas Huth

No need to declare a new variable in the the inner code block here, we can re-use the "ret" variable that has been declared at the beginning of the function. With this change, the code can now be successfully compiled with -Wshadow=local again.

Fixes a32e781838 ("Mark bdrv_snapshot_fallback() and callers GRAPH_RDLOCK")

---
v2: Assign "ret" only in one spot

  block/snapshot.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
block/snapshot.c +0 -1
index 6e16eb803a..55974273ae 100644
@@ -629,7 +629,6 @@ int bdrv_all_goto_snapshot(const char *name,
      while (iterbdrvs) {
          BlockDriverState *bs = iterbdrvs->data;
          AioContext *ctx = bdrv_get_aio_context(bs);
-        int ret = 0;
          bool all_snapshots_includes_bs;
 
          aio_context_acquire(ctx);
@@ -637,9 +636,8 @@ int bdrv_all_goto_snapshot(const char *name,
          all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs);

block/snapshot.c int bdrv_all_goto_snapshot(const char *name, · line 643

Quoted 10 lines
block/snapshot.c +2 -3
          bdrv_graph_rdunlock_main_loop();
 
-        if (devices || all_snapshots_includes_bs) {
-            ret = bdrv_snapshot_goto(bs, name, errp);
-        }
+        ret = (devices || all_snapshots_includes_bs) ?
+              bdrv_snapshot_goto(bs, name, errp) : 0;
          aio_context_release(ctx);
          if (ret < 0) {
              bdrv_graph_rdlock_main_loop();

Better. Unconditional assignment to @ret right before it's checked is how we should use @ret.

Quoted 6 lines
Quoted 3 lines

And queued. Thanks!

Mind if I drop the Fixes: tag? Nothing broken until we enable -Wshadow=local...

Fine for me!

Thomas

#4 lore raw
Quoted 44 lines · Thomas Huth
Quoted 43 lines · Markus Armbruster
Quoted 41 lines · Markus Armbruster
Quoted 25 lines · Thomas Huth

No need to declare a new variable in the the inner code block here, we can re-use the "ret" variable that has been declared at the beginning of the function. With this change, the code can now be successfully compiled with -Wshadow=local again.

Fixes a32e781838 ("Mark bdrv_snapshot_fallback() and callers GRAPH_RDLOCK")

---
  v2: Assign "ret" only in one spot

  block/snapshot.c | 6 ++----

  1 file changed, 2 insertions(+), 4 deletions(-)
block/snapshot.c +0 -1
index 6e16eb803a..55974273ae 100644
@@ -629,7 +629,6 @@ int bdrv_all_goto_snapshot(const char *name,
       while (iterbdrvs) {
           BlockDriverState *bs = iterbdrvs->data;
           AioContext *ctx = bdrv_get_aio_context(bs);
-        int ret = 0;
           bool all_snapshots_includes_bs;
           aio_context_acquire(ctx);

block/snapshot.c int bdrv_all_goto_snapshot(const char *name, · line 642

Quoted 10 lines
block/snapshot.c +2 -3
@@ -637,9 +636,8 @@ int bdrv_all_goto_snapshot(const char *name,
           all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs);
           bdrv_graph_rdunlock_main_loop();
-        if (devices || all_snapshots_includes_bs) {
-            ret = bdrv_snapshot_goto(bs, name, errp);
-        }
+        ret = (devices || all_snapshots_includes_bs) ?
+              bdrv_snapshot_goto(bs, name, errp) : 0;
           aio_context_release(ctx);
           if (ret < 0) {
               bdrv_graph_rdlock_main_loop();

Better.  Unconditional assignment to @ret right before it's checked is how we should use @ret.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

And queued.  Thanks!

Mind if I drop the Fixes: tag?  Nothing broken until we enable -Wshadow=local...

Fine for me!

This looks like the last remaining warning. Are we going to activate -Wshadow=local next ?

Thanks,

C.