transport-helper: avoid var decl in for () loop control
diff mbox series

Message ID xmqq36j53esa.fsf_-_@gitster-ct.c.googlers.com
State New
Headers show
Series
  • transport-helper: avoid var decl in for () loop control
Related show

Commit Message

Junio C Hamano July 16, 2019, 8:28 p.m. UTC
We do allow a few selected C99 constructs in our codebase these
days, but this is not among them (yet).

Reported-by: Carlo Arenas <carenas@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 transport.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Jonathan Nieder July 17, 2019, 12:42 a.m. UTC | #1
Junio C Hamano wrote:

> We do allow a few selected C99 constructs in our codebase these
> days, but this is not among them (yet).
>
> Reported-by: Carlo Arenas <carenas@gmail.com>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
>  transport.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)

Yes, gcc 4.8 fails to build without this:

 transport.c:1234:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
     for (struct ref *it = remote_refs; it; it = it->next)
     ^
 transport.c:1234:4: note: use option -std=c99 or -std=gnu99 to compile your code

Arguably it would be nice to use -std=gnu99 for better consistency
between gcc versions, but it's moot here: avoiding the declaration in
for loop initializer is more consistent with
-Wdeclaration-after-statement anyway.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Thanks.

Patch
diff mbox series

diff --git a/transport.c b/transport.c
index d768bc275e..453de8f704 100644
--- a/transport.c
+++ b/transport.c
@@ -1227,7 +1227,8 @@  int transport_push(struct repository *r,
 		ret = push_ret | err;
 
 		if ((flags & TRANSPORT_PUSH_ATOMIC) && err) {
-			for (struct ref *it = remote_refs; it; it = it->next)
+			struct ref *it;
+			for (it = remote_refs; it; it = it->next)
 				switch (it->status) {
 				case REF_STATUS_NONE:
 				case REF_STATUS_UPTODATE: