Skip to content

Commit f9974f7

Browse files
authored
[clang-tidy] Improve alternate snake case warnings (#71385)
Improves the accuracy of `readability-identifier-naming` for cases `Camel_Snake_Case` and `camel_Snake_Back`. Prior to this commit, these cases matched identifiers with **only** a leading upper case letter or leading lower case letter respectively. Now, uppercase letters can only appear at the start of an identifier or directly following an underscore. --- Currently, the regex for `Camel_Snake_Case` matches any identifier that starts with a capital letter: ``` ^[A-Z]([a-z0-9]*(_[A-Z])?)* ^^^^^^^^^-- underscore + capital letter after the first capital is optional ``` This means that `Camel_Snake_Case` matches other cases - in particular `CamelCase` and `Leading_upper_snake_case` - which causes clang-tidy to sometimes not flag incorrect casing. It also matches `UPPER_CASE`, but I think it's reasonable to consider this a subset of `Camel_Snake_Case` since some users may prefer e.g. `XML_Parser` to `Xml_Parser`. It's really easy to accidentally type an identifier that clang-tidy doesn't catch; all you have to do is omit an underscore or forget to capitalize a letter. The same problem also applies to `camel_Snake_Back` except that any identifier starting with a lower case letter matches, so I went ahead and adjusted its regex too. Fixing it also uncovered a minor error in an existing test.
1 parent 394bba7 commit f9974f7

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,8 @@ bool IdentifierNamingCheck::matchesStyle(
872872
llvm::Regex("^[a-z][a-zA-Z0-9]*$"),
873873
llvm::Regex("^[A-Z][A-Z0-9_]*$"),
874874
llvm::Regex("^[A-Z][a-zA-Z0-9]*$"),
875-
llvm::Regex("^[A-Z]([a-z0-9]*(_[A-Z])?)*"),
876-
llvm::Regex("^[a-z]([a-z0-9]*(_[A-Z])?)*"),
875+
llvm::Regex("^[A-Z]+([a-z0-9]*_[A-Z0-9]+)*[a-z0-9]*$"),
876+
llvm::Regex("^[a-z]+([a-z0-9]*_[A-Z0-9]+)*[a-z0-9]*$"),
877877
llvm::Regex("^[A-Z]([a-z0-9_]*[a-z])*$"),
878878
};
879879

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ Changes in existing checks
416416
``Leading_upper_snake_case`` naming convention. The handling of ``typedef``
417417
has been enhanced, particularly within complex types like function pointers
418418
and cases where style checks were omitted when functions started with macros.
419-
Added support for C++20 ``concept`` declarations.
419+
Added support for C++20 ``concept`` declarations. ``Camel_Snake_Case`` and
420+
``camel_Snake_Case`` now detect more invalid identifier names.
420421

421422
- Improved :doc:`readability-implicit-bool-conversion
422423
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// RUN: %check_clang_tidy -std=c++20 %s readability-identifier-naming %t -- \
2+
// RUN: -config='{CheckOptions: { \
3+
// RUN: readability-identifier-naming.ClassCase: Camel_Snake_Case, \
4+
// RUN: readability-identifier-naming.StructCase: camel_Snake_Back, \
5+
// RUN: }}'
6+
7+
// clang-format off
8+
9+
//===----------------------------------------------------------------------===//
10+
// Camel_Snake_Case tests
11+
//===----------------------------------------------------------------------===//
12+
class XML_Parser {};
13+
class Xml_Parser {};
14+
class XML_Parser_2 {};
15+
// NO warnings or fixes expected as these identifiers are Camel_Snake_Case
16+
17+
class XmlParser {};
18+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'XmlParser'
19+
// CHECK-FIXES: {{^}}class Xml_Parser {};{{$}}
20+
21+
class Xml_parser {};
22+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'Xml_parser'
23+
// CHECK-FIXES: {{^}}class Xml_Parser {};{{$}}
24+
25+
class xml_parser {};
26+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'xml_parser'
27+
// CHECK-FIXES: {{^}}class Xml_Parser {};{{$}}
28+
29+
class xml_Parser {};
30+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'xml_Parser'
31+
// CHECK-FIXES: {{^}}class Xml_Parser {};{{$}}
32+
33+
class xml_Parser_2 {};
34+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'xml_Parser_2'
35+
// CHECK-FIXES: {{^}}class Xml_Parser_2 {};{{$}}
36+
37+
class t {};
38+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 't'
39+
// CHECK-FIXES: {{^}}class T {};{{$}}
40+
41+
//===----------------------------------------------------------------------===//
42+
// camel_Snake_Back tests
43+
//===----------------------------------------------------------------------===//
44+
struct json_Parser {};
45+
struct json_Parser_2 {};
46+
struct u {};
47+
// NO warnings or fixes expected as these identifiers are camel_Snake_Back
48+
49+
struct JsonParser {};
50+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'JsonParser'
51+
// CHECK-FIXES: {{^}}struct json_Parser {};{{$}}
52+
53+
struct Json_parser {};
54+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'Json_parser'
55+
// CHECK-FIXES: {{^}}struct json_Parser {};{{$}}
56+
57+
struct json_parser {};
58+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'json_parser'
59+
// CHECK-FIXES: {{^}}struct json_Parser {};{{$}}
60+

clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ class my_other_templated_class : my_templated_class< my_class>, private my_deri
423423

424424
template<typename t_t>
425425
using mysuper_tpl_t = my_other_templated_class <:: FOO_NS ::my_class>;
426-
// CHECK-FIXES: {{^}}using mysuper_tpl_t = CMyOtherTemplatedClass <:: foo_ns ::CMyClass>;{{$}}
426+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for type alias 'mysuper_tpl_t'
427+
// CHECK-FIXES: {{^}}using mysuper_Tpl_t = CMyOtherTemplatedClass <:: foo_ns ::CMyClass>;{{$}}
427428

428429
const int global_Constant = 6;
429430
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: invalid case style for global constant 'global_Constant'

0 commit comments

Comments
 (0)