Re: [cocci] [1/36] coccinelle: misc: add cond_return_no_effect.cocci

Markus Elfring <[email protected]> Sun, 26 Jul 2026 13:30:43 +0200
Newsgroups gmane.linux.kernel.janitors,gmane.linux.kernel
Message-ID <[email protected]>
>>> +@depends on patch@
>>> +type T;
>>> +identifier collect.ret;
>>> +constant C;
>>> +@@
>>> +-      T ret =3D C;
>>> +       ... when !=3D ret
>>> +           when strict
>>> +
>>
>> While testing the script, I found that this rule can unexpectedly
>> remove the declaration of a global or static variable in the same
>> file, even when it is still used elsewhere. There is no such case in
>> this series, but I will fix it in v2 by matching the declaration
>> inside a function.
>=20
> I think you could avoid matching the path from the top of the function t=
o
> the declaration by doing the following:
>=20
> declaration D;
> statement S;
>=20
> (
> - T ret =3D C
> (
>   D
> |
>   S
> )
> &
>   T ret =3D C;
>   ... when !=3D ret
>       when strict
> )

I would appreciate to clarify such an approach for a bit of dead-code elim=
ination
in more detail.

I got the impression that some versions of the C programming language do
not handle variable declarations also as =E2=80=9Cstatements=E2=80=9D.
https://en.cppreference.com/c/language/statements

See also:
https://en.cppreference.com/cpp/language/statements#Declaration_statements


> The first part ensures that it is not a top-level declaration and the
> second part does the check you had previously.

I do not see so far that the intended source code restriction would be exp=
licitly
expressed here.

Will further software extensions become more helpful accordingly?

Regards,
Markus