Search code examples
c++clang-format

Example of clang-format getting qualifier placement wrong


Clang-format says about QualifierAlignment:

Warning

Setting QualifierAlignment to something other than Leave, COULD lead to incorrect code formatting due to incorrect decisions made due to clang-formats lack of complete semantic information. As such extra care should be taken to review code changes made by the use of this option.

I'd like to see example code of when this happens, so that I better know what I have to look out for in code reviews. I have tried several different options, from variables over parameters to const member functions and templates - clang-format got all of them right.


Solution

  • #define P char*
    
    P const p = nullptr;        // char* const p = nullptr;
    const P const s = nullptr;  // const char* const s = nullptr;
    

    QualifierAlignment: Left makes them semantically different.

    const P p = nullptr;        // const char* p = nullptr;
    const const P s = nullptr;  // const const char* s = nullptr;