Search code examples
c++regexclang-tidy

clang-tidy readability-identifier-naming IgnoredRegexp


I want to exclude certain regex patterns from linting for readability-identifier-naming.

Part of .clang-tidy I'm using:

Checks: 'readability-*'
CheckOptions:
  - key:             readability-identifier-naming.TypeAliasCase
    value:           CamelCase
  - key:             readability-identifier-naming.TypeAliasIgnoredRegexp
    value:           '(*_type|*reference|*iterator)'
  - key:             readability-identifier-naming.TypedefCase
    value:           CamelCase
  - key:             readability-identifier-naming.TypedefIgnoredRegexp
    value:           '(*_type|*reference|*iterator)'

However, warnings on those regex patterns are not suppressed.

For example,

using value_type = T;
using reference = value_type&;
using const_iterator = const T*;

Is this the right way to use regex for clang-tidy?


Solution

  • Looking at the changeset that introduced the IgnoredRegexp, it seems you have to use a proper regex - not a glob-match in the value-string

    %check_clang_tidy %s readability-identifier-naming %t -- 
      -config='{CheckOptions: [ 
        {key: readability-identifier-naming.ParameterCase, value: CamelCase}, 
        {key: readability-identifier-naming.ParameterIgnoredRegexp, value: "^[a-z]{1,2}$"}, 
        {key: readability-identifier-naming.ClassCase, value: CamelCase}, 
        {key: readability-identifier-naming.ClassIgnoredRegexp, value: "^fo$|^fooo$"}, 
        {key: readability-identifier-naming.StructCase, value: CamelCase}, 
        {key: readability-identifier-naming.StructIgnoredRegexp, value: "sooo|so|soo|$invalidregex["} 
     ]}'
    

    See: clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-ignored-regexp.cpp

    Guessing from your question and the samples, a pattern might look like this:

    value:           '^.*_type$|^.*reference$|^.*iterator$'
    

    If you need a non-greedy version you might be out of luck since the leveraged llvm::Regex class is using POSIX ERE and I guess lazy matching is not supported:

    value:           '^.*?_type$|^.*?reference$|^.*?iterator$'