CMake quality linting

CMake’s scripting language has some distinct characteristics as any late 1990’s language might. Editors such as Visual Studio Code have add-on CMake linting capabilities via CMake Tools. Deeper introspection is available via the CMake command-line options for uninitialized variables.

Uninitialized variables

Uninitialized variables can be less troublesome in CMake than other languages, but it’s almost always better to be explicit by setting a new variable. Check for CMake uninitialized variables by:

cmake --warn-uninitialized -B build

This warning option can have false positives, or at least what the developer might consider false positives. Often the warning can be stifled by doing like:



set(myopt "")

Unused variables

This warning would be helpful to indicate that the developer meant to set an option that wasn’t actually used. However, even the recent versions of CMake have a very large amount of false positive warnings with internal CMake variables that the user did not set. This makes this option perhaps not as useful for many projects.

cmake --warn-unused-var -B build