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

From: Bartosz Golaszewski

Date: Tue Oct 13 2026 - 07:44:50 EST


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.

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

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!

Bart