# \--warn-uninitialized Doesn't Warn When the Variable Is Unset

**URL:** https://discourse.cmake.org/t/warn-uninitialized-doesnt-warn-when-the-variable-is-unset/14200
**Category:** Development
**Created:** [June 4, 2025, 5:47pm UTC](https://discourse.cmake.org/t/warn-uninitialized-doesnt-warn-when-the-variable-is-unset/14200 "2025-06-04T17:47:29Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![trunketh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@trunketh](https://discourse.cmake.org/u/trunketh)
#### Post date: [June 4, 2025, 5:47pm UTC](https://discourse.cmake.org/t/warn-uninitialized-doesnt-warn-when-the-variable-is-unset/14200/1 "2025-06-04T17:47:29Z")

</div>

One thing that surprises me is that the [`--warn-uninitialized`](https://cmake.org/cmake/help/v4.0/manual/cmake.1.html#cmdoption-cmake-warn-uninitialized) flag does **not** issue a warning when `${X}` is used and the variable `X` has been explicitly unset using [`unset(X)`](https://cmake.org/cmake/help/v4.0/command/unset.html) or [`set(X)`](https://cmake.org/cmake/help/v4.0/command/set.html). For [example](https://godbolt.org/z/o5oK6b47q):

```cmake
if(NOT DEFINED X)
  message("'X' is undefined")
  message("X=${X}") # warning: uninitialized variable 'X'
endif()

unset(X) # or set(X)

if(NOT DEFINED X)
  message("'X' is undefined")
  message("X=${X}") # no warning
endif()

```

The relevant output is:

```auto
'X' is undefined
CMake Warning (dev) at example.cmake:5 (message):
  uninitialized variable 'X'
This warning is for project developers. Use -Wno-dev to suppress it.

X=
'X' is undefined
X=

```

It appears that the current behavior treats `unset(X)` (or `set(X)`) as if it **initializes** the variable `X`, even though it is actually **undefined**. 🤨 I find this behavior unintuitive and unhelpful. It also makes `--warn-uninitialized` inconsistent with [`if(DEFINED <variable>)`](https://cmake.org/cmake/help/v4.0/command/if.html#defined), as demonstrated above.

If the goal is to initialize a variable with an empty string, the appropriate and intuitive command should be `set(<variable> "")`. Moreover, `unset(X)` is **not** equivalent to `set(<variable> "")`, because unsetting a variable may expose a cache variable with the same name. So it remains unclear to me how the command `unset` can be logically associated with the concept of **initialization**.
