git/contrib/coccinelle/unused.cocci
Ævar Arnfjörð Bjarmason 4f40f6cb73 cocci: add and apply a rule to find "unused" strbufs
Add a coccinelle rule to remove "struct strbuf" initialization
followed by calling "strbuf_release()" function, without any uses of
the strbuf in the same function.

See the tests in contrib/coccinelle/tests/unused.{c,res} for what it's
intended to find and replace.

The inclusion of "contrib/scalar/scalar.c" is because "spatch" was
manually run on it (we don't usually run spatch on contrib).

Per the "buggy code" comment we also match a strbuf_init() before the
xmalloc(), but we're not seeking to be so strict as to make checks
that the compiler will catch for us redundant. Saying we'll match
either "init" or "xmalloc" lines makes the rule simpler.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-07-06 12:24:43 -07:00

33 lines
629 B
Plaintext

// This rule finds sequences of "unused" declerations and uses of a
// variable, where "unused" is defined to include only calling the
// equivalent of alloc, init & free functions on the variable.
@@
type T;
identifier I;
constant INIT_MACRO =~ "^STRBUF_INIT$";
identifier MALLOC1 =~ "^x?[mc]alloc$";
identifier INIT_CALL1 =~ "^strbuf_init$";
identifier REL1 =~ "^strbuf_(release|reset)$";
@@
(
- T I;
|
- T I = { 0 };
|
- T I = INIT_MACRO;
|
- T I = MALLOC1(...);
)
<... when != \( I \| &I \)
(
- \( INIT_CALL1 \)( \( I \| &I \), ...);
|
- I = MALLOC1(...);
)
...>
- \( REL1 \)( \( &I \| I \) );
... when != \( I \| &I \)