cat-file: declare loop counter inside for()
Declare loop counters in the for statement when they are only used within the loop body, limiting their scope and improving readability. While updating the loop counters, use size_t instead of int for counters that iterate over object counts. Update the 'nr' parameter of dispatch_calls() to size_t as all callers already pass a value of that type. Helped-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Eric Ju <eric.peijian@gmail.com> Signed-off-by: Pablo Sabater <pabloosabaterr@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Eric Ju committed
Jul 24, 2026 at 12:54 UTC
f05e099dea8ceeae5990f77ed6f4dc91d4be4694
2 files changed
+5
-11
builtin/cat-file.c
+4
-9
@@ -721,14 +721,12 @@ static void dispatch_calls(struct batch_options *opt,
721
struct strbuf *output,
722
struct expand_data *data,
723
struct queued_cmd *cmd,
724
- int nr)
724
+ size_t nr)
725
{
726
- int i;
727
-
726
if (!opt->buffer_output)
727
die(_("flush is only for --buffer mode"));
728
731
- for (i = 0; i < nr; i++)
729
+ for (size_t i = 0; i < nr; i++)
730
cmd[i].fn(opt, cmd[i].line, output, data);
731
732
fflush(stdout);
@@ -736,9 +734,7 @@ static void dispatch_calls(struct batch_options *opt,
734
735
static void free_cmds(struct queued_cmd *cmd, size_t *nr)
736
{
739
- size_t i;
740
-
741
- for (i = 0; i < *nr; i++)
737
+ for (size_t i = 0; i < *nr; i++)
738
FREE_AND_NULL(cmd[i].line);
739
740
*nr = 0;
@@ -765,7 +761,6 @@ static void batch_objects_command(struct batch_options *opt,
761
size_t alloc = 0, nr = 0;
762
763
while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
768
- int i;
764
const struct parse_cmd *cmd = NULL;
765
const char *p = NULL, *cmd_end;
766
struct queued_cmd call = {0};
@@ -775,7 +770,7 @@ static void batch_objects_command(struct batch_options *opt,
770
if (isspace(*input.buf))
771
die(_("whitespace before command: '%s'"), input.buf);
772
778
- for (i = 0; i < ARRAY_SIZE(commands); i++) {
773
+ for (size_t i = 0; i < ARRAY_SIZE(commands); i++) {
774
if (!skip_prefix(input.buf, commands[i].name, &cmd_end))
775
continue;
776
fetch-pack.c
+1
-2
@@ -1388,9 +1388,8 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1388
if (advertise_sid && server_supports_v2("session-id"))
1389
packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1390
if (server_options && server_options->nr) {
1391
- int i;
1391
ensure_server_supports_v2("server-option");
1393
- for (i = 0; i < server_options->nr; i++)
1392
+ for (size_t i = 0; i < server_options->nr; i++)
1393
packet_buf_write(req_buf, "server-option=%s",
1394
server_options->items[i].string);
1395
}