Re: [PATCH] lib: typecheck: initialize const variable to avoid Clang warning

From: Nathan Chancellor

Date: Wed Mar 25 2026 - 22:04:59 EST


On Sun, Mar 15, 2026 at 04:52:48PM +0800, hamjin wrote:
> Building the kernel with newer Clang versions triggers
> -Wdefault-const-init-var-unsafe in the typecheck() macro:
>
> error: default initialization of an object of type 'const unsigned long'
> leaves the object uninitialized [-Wdefault-const-init-var-unsafe]
>
> The warning originates from the following declaration in typecheck():
>
> typeof(x) __dummy2;
>
> When 'x' has a const-qualified type (e.g. 'const unsigned long'),
> Clang warns that the variable is declared const but not initialized.
> With -Werror enabled this causes the build to fail.
>
> Initialize the temporary variable to zero to silence the warning:
>
> typeof(x) __dummy2 = (typeof(x))0;
>
> This variable is only used for compile-time type checking and its
> value is never read, so the initialization has no functional impact
> on the generated code.
>
> Fixes: e0deaff47090 ("split the typecheck macros out of include/linux/kernel.h")
> Signed-off-by: hamjin <jinham@xxxxxx>
> ---
> include/linux/typecheck.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/include/linux/typecheck.h b/include/linux/typecheck.h
> index 46b15e2aaefb..09f280da5b52 100644
> --- a/include/linux/typecheck.h
> +++ b/include/linux/typecheck.h
> @@ -8,7 +8,7 @@
> */
> #define typecheck(type,x) \
> ({ type __dummy; \
> - typeof(x) __dummy2; \
> + typeof(x) __dummy2 = (typeof(x))0; \
> (void)(&__dummy == &__dummy2); \
> 1; \
> })
> --
> 2.53.0
>

As you can see from the build reports, this does not work. Are you
missing commit d0afcfeb9e38 ("kbuild: Disable
-Wdefault-const-init-unsafe") in your tree? Or does this appear in
somewhere that uses its own KBUILD_CFLAGS, in which case a fix like

5ba35a6c13ff ("s390/boot: Add -Wno-default-const-init-unsafe to KBUILD_CFLAGS")
b4780fe4ddf0 ("s390/purgatory: Add -Wno-default-const-init-unsafe to KBUILD_CFLAGS")

will be needed? I do not see any instances of this warning in Linus's
tree unless I am missing some configuration in my build tests.

Cheers,
Nathan