Re: [PATCH v1 1/1] gpiolib: shared: Don't define variable in the middle of the code

From: Andy Shevchenko

Date: Tue Oct 13 2026 - 11:21:29 EST


On Tue, Oct 13, 2026 at 02:42:59PM +0300, Bartosz Golaszewski wrote:
> On Tue, 13 Oct 2026 12:04:41 +0200, Jiri Slaby <jirislaby@xxxxxxxxxx> said:
> > On 13. 10. 26, 11:44, Andy Shevchenko wrote:
> >> While the C11 allows us to define variables in the code, we do not
> >> do this style (see more at [1]). Move the declaration to the top of
> >> the function.
> >>
> >> Link: https://lore.kernel.org/lkml/58fd478f408a34b578ee8d949c5c4b4da4d4f41d.camel@xxxxxxxxxxxxxxxxxxxxx/ [1]
> >
> > From the link, I have just learnt two things:
> > 1) I am not alone preferring declarations in the middle of blocks (it
> > has more pros than cons IMO). But again, I spend a lot of time in the
> > c++ universe.

True, but there is also old style and it's kept like this.

> I actually do prefer declaring variables at the top of the function. This is
> a leftover from when this variable was managed with __free().
>
> > 2) the kernel defines "auto" even for < c23. Therefore:

True, but using auto in the code will make it really hard to follow. I find it
nice in the macros, though.

> Excuse me, what?! :O
>
> That's the first time I'm learning that we can use C++-like auto in the kernel
> C code. Thanks for bringing this to my attention!

Yeah, with some version we eventually get it standardized.

--
With Best Regards,
Andy Shevchenko