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

From: Markus Elfring

Date: Sun Jul 26 2026 - 07:31:04 EST


>>> +@depends on patch@
>>> +type T;
>>> +identifier collect.ret;
>>> +constant C;
>>> +@@
>>> +- T ret = C;
>>> + ... when != 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.
>
> I think you could avoid matching the path from the top of the function to
> the declaration by doing the following:
>
> declaration D;
> statement S;
>
> (
> - T ret = C
> (
> D
> |
> S
> )
> &
> T ret = C;
> ... when != ret
> when strict
> )

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

I got the impression that some versions of the C programming language do
not handle variable declarations also as “statements”.
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 explicitly
expressed here.

Will further software extensions become more helpful accordingly?

Regards,
Markus