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

Julia Lawall <[email protected]> Sun, 26 Jul 2026 13:54:34 +0200 (CEST)
Newsgroups fr.inria.cocci,org.kernel.vger.kernel-janitors,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--8323329-1081996736-1785066874=:181691
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8BIT



On Sun, 26 Jul 2026, Markus Elfring wrote:

> >>> +@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 have no idea what you are talking about.  What dead code are you
referring to?

>
> 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

And the conclusion would be what?  First, Coccinelle has it's own parser.
It decides what is a statement, not some wikipedia page.

Second, Coccinelle does not consider declarations to be statements.
That's what the D is there for.

>
>
> > 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.

No idea what this means.  What intended code restriction are you referring
to?

julia

> Will further software extensions become more helpful accordingly?
>
> Regards,
> Markus
>
--8323329-1081996736-1785066874=:181691--