[clang] [C] Warn on uninitialized const objects (PR #137166)

Dan Liew via cfe-commits cfe-commits at lists.llvm.org
Thu May 1 11:16:53 PDT 2025


================
@@ -8197,6 +8197,16 @@ def err_address_space_qualified_new : Error<
 def err_address_space_qualified_delete : Error<
   "'delete' cannot delete objects of type %0 in address space '%1'">;
 
+def note_default_init_const_member : Note<
+  "member %0 declared 'const' here">;
+def warn_default_init_const : Warning<
+  "default initialization of an object of type %0%select{| with const member}1 "
+  "is incompatible with C++">,
+  InGroup<DefaultConstInit>, DefaultIgnore;
+def warn_default_init_const_unsafe : Warning<
+  "default initialization of an object of type %0%select{| with const member}1 "
+  "leaves the object uninitialized and is incompatible with C++">,
----------------
delcypher wrote:

@AaronBallman I just came across this diagnostic. Why did we chose to mention the incompatibility with C++? I found it odd because the diagnostic is showing up when building a C source file. If the code was in an inline function in a header (that could be consumed from C++) then I think it would make sense to mention incompatibility with C++ but if it's in a C source file it makes less sense to me. When I'm building C source files I don't typically think (or care) about what would happen if I were to build the source file with C++ instead of C.

Really not a huge deal but I'd like to understand the reasoning and here.

https://github.com/llvm/llvm-project/pull/137166


More information about the cfe-commits mailing list