Search code examples
c++clang-tidy

Why clang-tidy readability-isolate-declaration not fixing code?


I tried to use readability-isolate-declaration check in clang-tidy, but nothing fixed. Example of code from test.cpp file:

void f() {
    int a = 0, b = 1, c = 2;
}

What I've done:

clang-tidy -checks='readability-isolate-declaration' -fix test.cpp

Output:

Error while trying to load a compilation database:
Could not auto-detect compilation database for file "test.cpp"
No compilation database found in /home/anzipex/Downloads/clang-test or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.

Solution

  • There are several problems here:

    1. readability-isolate-declaration check didn't exist in clang-tidy 6. Upgrade to a more recent version
    2. If you don't have a compilation database, you can use --(double dash) to specify compilation options. Even if you specify none, this will tell clang-tidy to compile the file. See the documentation.
    3. You didn't tell clang-tidy to exclude other checks besides the one you want

    This is what the command should look like:

    clang-tidy -checks='-*,readability-isolate-declaration' test.cpp -fix --

    Output:

    void f() {
        int a = 0;
        int b = 1;
        int c = 2;
    }