首页 > 解决方案 > clang-tidy 可读性标识符命名 IgnoredRegexp

问题描述

我想从 linting for 中排除某些正则表达式模式readability-identifier-naming

.clang-tidy我正在使用的一部分:

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)'

但是,不会抑制对这些正则表达式模式的警告。

例如,

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

这是使用正则表达式的正确方法clang-tidy吗?

标签: c++regexclang-tidy

解决方案


查看引入的变更集IgnoredRegexp,您似乎必须使用正确的正则表达式 - 而不是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["} 
 ]}'

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

根据您的问题和示例猜测,模式可能如下所示:

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

如果你需要一个非贪婪的版本,你可能会不走运,因为杠杆llvm::Regex类使用的是 POSIX ERE,我猜不支持惰性匹配:

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

推荐阅读