Re: [PATCH v1 1/1] gpiolib: shared: Don't define variable in the middle of the code
From: Jiri Slaby
Date: Tue Oct 13 2026 - 06:16:47 EST
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.
2) the kernel defines "auto" even for < c23. Therefore:
Signed-off-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
---
drivers/gpio/gpiolib-shared.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/gpio/gpiolib-shared.c b/drivers/gpio/gpiolib-shared.c
index 5f9623e40b0f..40c08f1ae5f0 100644
--- a/drivers/gpio/gpiolib-shared.c
+++ b/drivers/gpio/gpiolib-shared.c
@@ -196,6 +196,7 @@ static int gpio_shared_of_traverse(struct device_node *curr)
for (i = 0; i < count; i++) {
struct device_node *np __free(device_node) = NULL;
char *con_id __free(kfree) = NULL;
+ struct fwnode_handle *curr_fwnode;
ret = of_parse_phandle_with_args(curr, prop->name,
"#gpio-cells", i,
@@ -261,8 +262,7 @@ static int gpio_shared_of_traverse(struct device_node *curr)
con_id[con_id_len - suffix_len] = '\0';
}
- struct fwnode_handle *curr_fwnode =
- fwnode_handle_get(of_fwnode_handle(curr));
+ curr_fwnode = fwnode_handle_get(of_fwnode_handle(curr));
For me, much cleaner would be the c++ way. Ie. doing:
auto curr_fwnode = fwnode_handle_get(of_fwnode_handle(curr));
Everyone is supposed to use clangd, tree-sitter or alike nowadays. So inferring the type is the very same for start-of-block-declaration (which is miles away from the assignment now) and auto-declaration. While the latter obfuscates the code less (and eases local vars addition w/o jumping back and forth).
ref = gpio_shared_make_ref(curr_fwnode, con_id, args.args[1]);
if (!ref) {
fwnode_handle_put(curr_fwnode);
thanks,
--
js
suse labs