# [PATCH] attr.c: simplify macroexpand_one()

4 messages from 2016-05-15 to 2016-05-16. Participants: Junio C Hamano, Eric Sunshine, Stefan Beller.
Thread: https://gitlist.dev/t/42332

## Junio C Hamano, 2016-05-15 22:57

Subject: [PATCH] attr.c: simplify macroexpand_one()
Message-ID: <xmqqmvnr3p62.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqmvnr3p62.fsf%40gitster.mtv.corp.google.com

```
The double-loop wants to do an early return immediately when one
matching macro is found.  Eliminate the extra variable 'a' used for
that purpose and rewrite the "assign found itme to 'a' to make it
non-NULL and force the loop(s) to terminate" with a direct return
from there.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 attr.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/attr.c b/attr.c
index 95416d3..7bfeef3 100644
--- a/attr.c
+++ b/attr.c
@@ -701,24 +701,21 @@ static int fill(const char *path, int pathlen, int basename_offset,
 static int macroexpand_one(int nr, int rem)
 {
 	struct attr_stack *stk;
-	struct match_attr *a = NULL;
 	int i;
 
 	if (check_all_attr[nr].value != ATTR__TRUE ||
 	    !check_all_attr[nr].attr->maybe_macro)
 		return rem;
 
-	for (stk = attr_stack; !a && stk; stk = stk->prev)
-		for (i = stk->num_matches - 1; !a && 0 <= i; i--) {
+	for (stk = attr_stack; stk; stk = stk->prev) {
+		for (i = stk->num_matches - 1; 0 <= i; i--) {
 			struct match_attr *ma = stk->attrs[i];
 			if (!ma->is_macro)
 				continue;
 			if (ma->u.attr->attr_nr == nr)
-				a = ma;
+				return fill_one("expand", ma, rem);
 		}
-
-	if (a)
-		rem = fill_one("expand", a, rem);
+	}
 
 	return rem;
 }
-- 
2.8.2-748-gfb85f76

```

## Eric Sunshine, 2016-05-16 03:31

Subject: Re: [PATCH] attr.c: simplify macroexpand_one()
Message-ID: <CAPig+cST7phEvTodEDoYk2UpDOssKg9OVcpddT0sZPy-0aMqsg@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcST7phEvTodEDoYk2UpDOssKg9OVcpddT0sZPy-0aMqsg%40mail.gmail.com
In-Reply-To: <xmqqmvnr3p62.fsf@gitster.mtv.corp.google.com>

```
On Sun, May 15, 2016 at 6:57 PM, Junio C Hamano <gitster@pobox.com> wrote:
> The double-loop wants to do an early return immediately when one
> matching macro is found.  Eliminate the extra variable 'a' used for
> that purpose and rewrite the "assign found itme to 'a' to make it

What's "itme"?

> non-NULL and force the loop(s) to terminate" with a direct return
> from there.
>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>

```

## Junio C Hamano, 2016-05-16 04:41

Subject: Re: [PATCH] attr.c: simplify macroexpand_one()
Message-ID: <CAPc5daVH_qveYBnxybvZYTuVKNBhG7PnJxeY9f8zVXqNxpvzVQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAPc5daVH_qveYBnxybvZYTuVKNBhG7PnJxeY9f8zVXqNxpvzVQ%40mail.gmail.com
In-Reply-To: <CAPig+cST7phEvTodEDoYk2UpDOssKg9OVcpddT0sZPy-0aMqsg@mail.gmail.com>

```
On Sun, May 15, 2016 at 8:31 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sun, May 15, 2016 at 6:57 PM, Junio C Hamano <gitster@pobox.com> wrote:
>> The double-loop wants to do an early return immediately when one
>> matching macro is found.  Eliminate the extra variable 'a' used for
>> that purpose and rewrite the "assign found itme to 'a' to make it
>
> What's "itme"?

item.

```

## Stefan Beller, 2016-05-16 16:13

Subject: Re: [PATCH] attr.c: simplify macroexpand_one()
Message-ID: <CAGZ79kanUufPKHK3-QGoTK8jju_iRGokpXpR4-7Zwc+tAodC4A@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79kanUufPKHK3-QGoTK8jju_iRGokpXpR4-7Zwc%2BtAodC4A%40mail.gmail.com
In-Reply-To: <CAPc5daVH_qveYBnxybvZYTuVKNBhG7PnJxeY9f8zVXqNxpvzVQ@mail.gmail.com>

```
On Sun, May 15, 2016 at 9:41 PM, Junio C Hamano <gitster@pobox.com> wrote:
> On Sun, May 15, 2016 at 8:31 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
>> On Sun, May 15, 2016 at 6:57 PM, Junio C Hamano <gitster@pobox.com> wrote:
>>> The double-loop wants to do an early return immediately when one
>>> matching macro is found.  Eliminate the extra variable 'a' used for
>>> that purpose and rewrite the "assign found itme to 'a' to make it
>>
>> What's "itme"?
>
> item.

The code looks good.

Thanks,
Stefan

```
