Search code examples
c++clangclang-tidy

Clang tidy doesn't work properly for static member variable


I am running clang-tidy for my project. Here are the relevant naming options from my .clang-tidy file:

- key:             readability-identifier-naming.ClassMemberCase # e.g., int myClassMember_
  value:           camelBack
- key:             readability-identifier-naming.ClassMemberSuffix # e.g., int myClassMember_
  value:           _
- key:             readability-identifier-naming.GlobalConstantCase # e.g., int MyGlobalConstant (please don't make globals)
  value:           CamelCase
- key:             readability-identifier-naming.IgnoreMainLikeFunctions # Doesn't apply clang checks to main() args
  value:           1
- key:             readability-identifier-naming.MacroDefinitionCase # e.g., #define MY_MACRO="PleaseDon'tUseMacrosAnywhere"
  value:           UPPER_CASE
- key:             readability-identifier-naming.MacroDefinitionIgnoredRegexp
  value:           '^[A-Z]+(_[A-Z]+)*_$'
- key:             readability-identifier-naming.MemberCase # e.g., int myMember_ = 42;
  value:           CamelCase
- key:             readability-identifier-naming.MemberSuffix # e.g., int myMember_ = 42;
  value:           _
- key:             readability-identifier-naming.ParameterCase # e.g., void MyFunction(int parameter, int anotherParam);
  value:           camelBack
- key:             readability-identifier-naming.StaticConstantCase # e.g., static const std::string s_myStaticConstant = "I hope this works!"
  value:           camelBack
- key:             readability-identifier-naming.StaticConstantPrefix # e.g., static const std::string s_myStaticConstant = "I hope this works!"
  value:           s_
- key:             readability-identifier-naming.StaticVariableCase # e.g., static std::string s_myStaticVar = "I hope this works!"
  value:           camelBack
- key:             readability-identifier-naming.StaticVariablePrefix # e.g., static std::string s_myStaticVar = "I hope this works!"
  value:           s_
- key:             readability-identifier-naming.StructCase # e.g., struct MyStruct { ... };
  value:           CamelCase
- key:             readability-identifier-naming.VariableCase # e.g., int myVariable = 10;
  value:           camelBack

Unfortunately, clang tidy turns my static class member variables into sMyVariable_ instead of s_myVariable_. It seems as if the options for class members are overriding the options for static variables:

warning: invalid case style for class member 's_myVariable_' [readability-identifier-naming]
int MyClass::s_myVariable_ = 1;

Is there any way to have the static naming rules prioritized over the member naming rules? Thanks!

Example code:

class MyClass{
   static int s_myVariable_; // Clang-tidy doesn't like this
};

Solution

  • I had a hard time finding this in the documentation as well. It appears that static member variable readability-identifier-naming styles are defined with ClassMemberCase, ClassMemberPrefix, and ClassMemberSuffix.

    For your desired formatting

    class MyClass{
       static int s_myVariable_; // Clang-tidy doesn't like this
    };
    

    the CheckOptions field of the .clang-tidy file would have the follwing key/values:

      - { key: readability-identifier-naming.ClassMemberPrefix,   value: s_  }
      - { key: readability-identifier-naming.ClassMemberCase,     value: camelBack  }
      - { key: readability-identifier-naming.ClassMemberSuffix,   value: _  }