I was hoping to use clang-tidy’s --export-fixes option in conjunction with Clang warnings fix-its to address a bunch of Clang warnings in our codebase in an automated fashion. (Using Clang’s fix-it options directly isn’t an option because some of the warnings are in headers, so there’s potential issues with concurrent modifications.)
My issue is that I only want fixes to be generated for the specific warning I’m trying to fix. I can get most of the way there with something like:
However, this still leaves clang-tidy’s default checks in place, so I might get fixes from those as well. If I attempt to turn off all of those using --checks='-*', I get “Error: no checks enabled.”. I can’t figure out how to only enable checks associated with Clang diagnostics; I noticed that the warning in question is labeled as clang-diagnostic-unqualified-std-cast-call, but --checks='-*,clang-diagnostic-*' still gave the same no checks enabled error.
Is there any way to get clang-tidy to only generate fixes for a particular Clang warning and no other checks? I can always post-process the fixes YAML file if not, but it would be convenient to not have to do that.
You can try disabling the clang-tidy checks by module instead of -*, e.g. -readability-*,-modernize-* etc. Experiment with --list-checks to get the filter the way you like it.
Side note: when I run clang-tidy --list-checks with no other checks enabled, I only see the clang-analyzer-* checks listed. Do you have a .clang-tidy file in your project that is enabling other checks?
Thanks! I also see the same when I run --list-checks without anything else, but I wasn’t sure if any of the analyzer checks had their own potential fixits. I suppose I could only turn on a single diagnostic which is very unlikely to have its own fix-it, but that feels kinda hacky, and it’d be nicer to have an explicit way to select “only turn on Clang diagnostics and turn off everything else”.
If I understand it correctly, you want to fix compiler warnings one by one via fix-it. To disable all warnings, there is -w. I believe clang parses arguments from left to right and you might be lucky that this works together.
Another alternative is to create a header that suppresses all warnings you have and add that as extra include via the command line.
In order to fix it, clang has some -Xclang -fixit option. This will update the code while compiling it. It will fix the file for you during compilation. The disadvantage is that you need to run that single-process as you otherwise have problems when 2 versions fix the same header at the same time.
-w will negate any explicit -W even if those are later on the command line, but I’m using -Wno-everything -Wmy-warning at the end of hte command line to achieve the same effect. I also want to use clang-tidy instead of -Xclang -fixit precisely to avoid the header concurrency issue. I was hoping for a way to make clang-tidy enable clang-diagnostic checks and disable everything else, so that I can guarantee that the generated fix-its are only for the warning I’m interested in.