Re: [PATCH] refactor various if (x) FREE_AND_NULL(x) to just FREE_AND_NULL(x)
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Aug 17, 2018, 17:07 UTC
- Message-ID
- <xmqqlg94c46f.fsf@gitster-ct.c.googlers.com>
- In-Reply-To
- <xmqqpnyhaq93.fsf@gitster-ct.c.googlers.com>
Junio C Hamano <gitster@pobox.com> writes:
Show 8 quoted lines
> It is a bit sad that > > - if (E) > FREE_AND_NULL(E); > > is not sufficient to catch it. Shouldn't we be doing the same for > regular free(E) as well? IOW, like the attached patch. > ...
And revised even more to also spell "E" as "E != NULL" (and "!E" as "E == NULL"), which seems to make a difference, which is even more sad. I do not want to wonder if I have to also add "NULL == E" and other variants, so I'll stop here.
contrib/coccinelle/free.cocci | 60 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+)
diff --git a/contrib/coccinelle/free.cocci b/contrib/coccinelle/free.cocci index 4490069df9..29ca98796f 100644 --- a/contrib/coccinelle/free.cocci +++ b/contrib/coccinelle/free.cocci @@ -16,3 +16,63 @@ expression E; - free(E); + FREE_AND_NULL(E); - E = NULL; + +@@ +expression E; +@@ +- if (E) + FREE_AND_NULL(E); + +@@ +expression E; +@@ +- if (E != NULL) + free(E); + +@@ +expression E; +@@ +- if (E == NULL) + free(E); + +@@ +expression E; +@@ +- if (E != NULL) + FREE_AND_NULL(E); + +@@ +expression E; +@@ +- if (E) { free(E); } ++ free(E); + +@@ +expression E; +@@ +- if (!E) { free(E); } ++ free(E); + +@@ +expression E; +@@ +- if (E) { FREE_AND_NULL(E); } ++ FREE_AND_NULL(E); + +@@ +expression E; +@@ +- if (E != NULL) { free(E); } ++ free(E); + +@@ +expression E; +@@ +- if (E == NULL) { free(E); } ++ free(E); + +@@ +expression E; +@@ +- if (E != NULL) { FREE_AND_NULL(E); } ++ FREE_AND_NULL(E);