# clang-tidy for CMake?

**URL:** https://discourse.cmake.org/t/clang-tidy-for-cmake/10188
**Category:** Code
**Created:** [February 26, 2024, 12:56am UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188 "2024-02-26T00:56:32Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![cen](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/ce7236/32.png) [@cen](https://discourse.cmake.org/u/cen)
#### Post date: [February 26, 2024, 12:56am UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188/1 "2024-02-26T00:56:32Z")

</div>

Imagine: clang-tidy CMakeLists.txt -checks=modern-\*

There are many blogs and videos out there telling us how to write modern CMake but having the mental capacity to stick to those guidelines is a different story.

Is there anything out there that would spew a warning on a bad practice? I am quite impressed how clang-tidy can suggest effective improvements to C++ code so doing the same for CMake code does not seem too far fetched.

---

<div class="post-metadata">

### Author: ![Lecris](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lecris/32/3193_2.png) [@Lecris](https://discourse.cmake.org/u/Lecris)
#### Post date: [February 27, 2024, 9:21am UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188/2 "2024-02-27T09:21:58Z")

</div>

The closest discussion is: [CMake linter?](https://discourse.cmake.org/t/cmake-linter/8391)

---

<div class="post-metadata">

### Author: ![Geoffrey\_Hoffmann](https://discourse.cmake.org/user_avatar/discourse.cmake.org/geoffrey_hoffmann/32/4224_2.png) [@Geoffrey\_Hoffmann](https://discourse.cmake.org/u/Geoffrey_Hoffmann)
#### Post date: [February 28, 2024, 7:17am UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188/3 "2024-02-28T07:17:23Z")

</div>

If you’re using VSCode, the plug-in for CMake has a nice CMake-tidy extension.

---

<div class="post-metadata">

### Author: ![codeling](https://discourse.cmake.org/user_avatar/discourse.cmake.org/codeling/32/1700_2.png) [@codeling](https://discourse.cmake.org/u/codeling)
#### Post date: [February 28, 2024, 11:22am UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188/4 "2024-02-28T11:22:04Z")

</div>

Which plug-in/extension do you mean exactly? I tried installing the “CMake Tools” by Microsoft and “CMake” by twxs, but haven’t found anything about tidying (just nice syntax highlighting). Could you elaborate a bit on how to access this feature?

Note that I’ve only very recently started to use VSCode, so thanks in advance for your help!

---

<div class="post-metadata">

### Author: ![Geoffrey\_Hoffmann](https://discourse.cmake.org/user_avatar/discourse.cmake.org/geoffrey_hoffmann/32/4224_2.png) [@Geoffrey\_Hoffmann](https://discourse.cmake.org/u/Geoffrey_Hoffmann)
#### Post date: [February 28, 2024, 11:37am UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188/5 "2024-02-28T11:37:55Z")

</div>

If you right click inside a CMake list file, you’ll see a couple of options for “Format Document”. You can play around with them.

Get [BlueMail for Android](https://bluemail.me)

---

<div class="post-metadata">

### Author: ![Geoffrey\_Hoffmann](https://discourse.cmake.org/user_avatar/discourse.cmake.org/geoffrey_hoffmann/32/4224_2.png) [@Geoffrey\_Hoffmann](https://discourse.cmake.org/u/Geoffrey_Hoffmann)
#### Post date: [February 28, 2024, 11:38am UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188/6 "2024-02-28T11:38:32Z")

</div>

I mean left click, not right click…

Get [BlueMail for Android](https://bluemail.me)

---

<div class="post-metadata">

### Author: ![Geoffrey\_Hoffmann](https://discourse.cmake.org/user_avatar/discourse.cmake.org/geoffrey_hoffmann/32/4224_2.png) [@Geoffrey\_Hoffmann](https://discourse.cmake.org/u/Geoffrey_Hoffmann)
#### Post date: [February 28, 2024, 11:39am UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188/7 "2024-02-28T11:39:15Z")

</div>

I should quit while I’m ahead. It is right click.

Get [BlueMail for Android](https://bluemail.me)

---

<div class="post-metadata">

### Author: ![cen](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/ce7236/32.png) [@cen](https://discourse.cmake.org/u/cen)
#### Post date: [February 28, 2024, 11:45am UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188/8 "2024-02-28T11:45:18Z")

</div>

Format and syntax highlighting is one thing but actual best-practice linter is a different story which is what I am talking about.

The most basic example would be: you have a include\_directories directive and the linter would produce a warning suggesting to use target\_ include\_directories instead.

---

<div class="post-metadata">

### Author: ![codeling](https://discourse.cmake.org/user_avatar/discourse.cmake.org/codeling/32/1700_2.png) [@codeling](https://discourse.cmake.org/u/codeling)
#### Post date: [February 28, 2024, 11:45am UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188/9 "2024-02-28T11:45:36Z")

</div>

Just with CMake and CMake Tools extension, I don’t have any additional options in the right click menu, it looks exactly as for other files. I looked a bit more now, and found the “cmake-format” extension. It provides the mentioned “Format Document” right click menu entry, however that doesn’t seem to do anything - after clicking that, there is no change whatsoever in the file…

Edit: Seems the “cmake-format” extension is just a bridge for calling the `cmake-format` tool (probably the one from [cheshirekow/cmake\_format @ github](https://github.com/cheshirekow/cmake_format) - looks like this also contains a `cmake-lint`! Or is it a part of that collection? They also provide a vsix file under release asses 😕 But the project seems a bit abandoned, no change since 3 years

---

<div class="post-metadata">

### Author: ![Geoffrey\_Hoffmann](https://discourse.cmake.org/user_avatar/discourse.cmake.org/geoffrey_hoffmann/32/4224_2.png) [@Geoffrey\_Hoffmann](https://discourse.cmake.org/u/Geoffrey_Hoffmann)
#### Post date: [February 28, 2024, 12:01pm UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188/10 "2024-02-28T12:01:37Z")

</div>

Yeah, there are two entries when I right click

One says “Format Document [Ctrl+K Ctrl+D]”

The other says "Format Document With… "

Selecting the ‘with’ one, I get to formatters to choose from, “CMake-format” which does nothing, and another that says “CMake Language Support” and that’s the one that works…

Get [BlueMail for Android](https://bluemail.me)

---

<div class="post-metadata">

### Author: ![Geoffrey\_Hoffmann](https://discourse.cmake.org/user_avatar/discourse.cmake.org/geoffrey_hoffmann/32/4224_2.png) [@Geoffrey\_Hoffmann](https://discourse.cmake.org/u/Geoffrey_Hoffmann)
#### Post date: [February 28, 2024, 12:04pm UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188/11 "2024-02-28T12:04:03Z")

</div>

Yes, I see what you mean. I haven’t seen anything as capable as that.

Get [BlueMail for Android](https://bluemail.me)

---

<div class="post-metadata">

### Author: ![codeling](https://discourse.cmake.org/user_avatar/discourse.cmake.org/codeling/32/1700_2.png) [@codeling](https://discourse.cmake.org/u/codeling)
#### Post date: [February 28, 2024, 12:38pm UTC](https://discourse.cmake.org/t/clang-tidy-for-cmake/10188/12 "2024-02-28T12:38:35Z")

</div>

As far as I can tell, the cmake-lint command I mentioned above is mostly reporting on what it considers formatting problems. It has a few style remarks as well (regarding variable, function and argument names), but I don’t know from which recomendations it pulls its recommendations. For example, for this line:

```auto
function (ExtractVersion filename identifier output_varname)

```

it brings up this message:

```auto
Invalid function name "ExtractVersion" doesn't match `[0-9a-z_]+`.

```

Apart from the formatting messages and such low level coding style remarks, I don’t see any “more useful” output at the moment, for example regarding modern CMake.
