Skip to contentSkip to conversation

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

PATCH Superseded Thomas Huth 2 participants 3 messages Oct 23, 2023 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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")
---
 block/snapshot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/snapshot.c +1 -1
index 6e16eb803a..50adf5381e 100644
@@ -629,8 +629,8 @@ 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;
+        ret = 0;
 
         aio_context_acquire(ctx);
         bdrv_graph_rdlock_main_loop();
Signature · 1 line
2.41.0
#1 lore raw
Quoted 16 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")
---
 block/snapshot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/snapshot.c +0 -0
index 6e16eb803a..50adf5381e 100644

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

Quoted 5 lines
block/snapshot.c +0 -1
@@ -629,8 +629,8 @@ 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;

Blank line between declarations and statements, please.

I'm not sure we actually need the assignment. Proving we don't looks like a poor use of our time, though.

I recommend to move the assignment from here...

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

Quoted 4 lines
block/snapshot.c +1 -0
+        ret = 0;
 
         aio_context_acquire(ctx);
         bdrv_graph_rdlock_main_loop();
           all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs);
           bdrv_graph_rdunlock_main_loop();

... down to here.

           if (devices || all_snapshots_includes_bs) {
               ret = bdrv_snapshot_goto(bs, name, errp);
           }
           aio_context_release(ctx);
           if (ret < 0) {

We lose the symmetry with the other three while (iterbdrvs) loops. Do we care?

#2 lore raw
Quoted 26 lines · Markus Armbruster
Quoted 16 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")
---
  block/snapshot.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
block/snapshot.c +0 -0
index 6e16eb803a..50adf5381e 100644

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

Quoted 5 lines
block/snapshot.c +0 -1
@@ -629,8 +629,8 @@ 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;

Blank line between declarations and statements, please.

Quoted 2 lines

I'm not sure we actually need the assignment. Proving we don't looks like a poor use of our time, though.

I stared at the code for a while, and I think we don't urgently need it. But I'd still recommend to rather keep it to render the code more robust for future changes (imagine someone adds some code that changes ret in between, but does not return on negative values...)

Quoted 6 lines

I recommend to move the assignment from here...

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

Quoted 4 lines
block/snapshot.c +1 -0
+        ret = 0;
 
          aio_context_acquire(ctx);
          bdrv_graph_rdlock_main_loop();
Quoted 8 lines
            all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs);
            bdrv_graph_rdunlock_main_loop();

... down to here.

            if (devices || all_snapshots_includes_bs) {
                ret = bdrv_snapshot_goto(bs, name, errp);
            }

IMHO this would look best in my eyes:

        ret = (devices || all_snapshots_includes_bs) ?
              bdrv_snapshot_goto(bs, name, errp) : 0;

I'll send a v2 with this change.

Quoted 5 lines
            aio_context_release(ctx);
            if (ret < 0) {

We lose the symmetry with the other three while (iterbdrvs) loops. Do we care?

No, at least I don't ;-)

Thomas