cat-file: remove definition of already defined variables
PATCH 1/1 cat-file: remove definition of already defined variables
'enum object_type type' and 'unsigned long size' are already defined at the top of cat_one_file routine
--- builtin/cat-file.c | 2 -- 1 file changed, 2 deletions(-)
builtin/cat-file.c +0 -2
@@ -75,8 +75,6 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)if (type_from_string (exp_type) ==OBJ_BLOB ) {unsigned char blob_sha1[20 ];if (sha1_object_info (sha1,NULL ) ==OBJ_TAG ) { -enum object_type type; -unsigned long size;char *buffer =read_sha1_file (sha1, &type, &size);const char *target; if (!skip_prefix(buffer, "object ", &target) ||
Signature · 1 line
2.2.1.532.g168b885.dirty
Quoted 2 lines · Alexander Kuleshov
'enum
object_typetype' and 'unsigned long size' are already defined at the top ofcat_one_fileroutine
True. That description applies to two completely different situations, though, and does not help judging if the change is correct.
Scenario #1:
unsigned long size;
size = something;
if (...) {
unsigned long size = something else;
}
use(size);Scenario #2:
unsigned long size;
size = something;
if (...) {
unsigned long size = something else;
}
size = yet another something else;
use(size);In either case, the variable definition in the inner block hides the variable that happens to share the same name, but in Scenario #1, the hiding is deliberate and reusing the variable from outside would break the later use of the variable.
In the code in question, the outer variable's value before the block is not used after the block, so it is safe to remove the inner definition and reuse the variable from the outer block.
"already defined at the top" does not help the readers to tell that we are dealing with the Scenario #2 here.
Thanks, will apply after trying to come up with a better way to phrase this.
Quoted 18 lines
Signed-off-by Alexander Kuleshov <[email protected]>--- builtin/cat-file.c | 2 -- 1 file changed, 2 deletions(-)builtin/cat-file.c +0
-2@@ -75,8 +75,6 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)if (type_from_string (exp_type) ==OBJ_BLOB ) {unsigned char blob_sha1[20 ];if (sha1_object_info (sha1,NULL ) ==OBJ_TAG ) { -enum object_type type; -unsigned long size;char *buffer =read_sha1_file (sha1, &type, &size);const char *target; if (!skip_prefix(buffer, "object ", &target) ||