Re: Clarifying confusion of our variable placement rules caused by cleanup.h

From: H. Peter Anvin

Date: Tue Nov 18 2025 - 14:15:53 EST


On 2025-11-18 11:04, Bart Van Assche wrote:
> On 11/18/25 10:38 AM, Linus Torvalds wrote:
>> Now, we currently don't use __auto_type very much outside of macros
>> (and there we often use "typeof(x)" instead for historical compiler
>> reasons), but I suspect we probably should.  There's a patch floating
>> around that makes it more convenient with a
>>
>>     #define auto __auto_type
>>
>> because the historical C 'auto' keyword has been so completely and
>> utterly useless.
>
> In a C++ style guide I found the following advice for type deduction:
>
> "Use type deduction only if it makes the code clearer to readers who
> aren't familiar with the project, or if it makes the code safer. Do not
> use it merely to avoid the inconvenience of writing an explicit type."
>
> However, I'm not sure whether this guidance also makes sense for C kernel
> code. See also
> https://google.github.io/styleguide/cppguide.html#Type_deduction
>

The "makes code clearer or safer" seems like a good idea to me.

Notably the following constructs, mostly used in macros:

typeof(x) _x = (x);
typeof(z) _y = (typeof(z)) (y);

... are really quite dangerous because it is very easy to mistakenly put the
wrong variable inside the typeof().

-hpa