Skip to content

Commit 8f341cc

Browse files
zeyi2EugeneZelenkounterumarmung
authored
[clang-tidy] Fix readability-identifier-naming FP with DefaultCase on function templates (llvm#189788)
Closes llvm#189755 --------- Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com> Co-authored-by: Daniil Dudkin <unterumarmung@yandex.ru>
1 parent 524525e commit 8f341cc

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,13 @@ StyleKind IdentifierNamingCheck::findStyleKind(
13031303
return SK_Function;
13041304
}
13051305

1306+
// Ignore template wrapper decls. Their underlying decls are checked with the
1307+
// right naming kind, checking the wrappers too can fall back to DefaultCase
1308+
// and emit diagnostic for the same identifier.
1309+
if (isa<FunctionTemplateDecl, ClassTemplateDecl, VarTemplateDecl,
1310+
TypeAliasTemplateDecl>(D))
1311+
return SK_Invalid;
1312+
13061313
if (isa<TemplateTypeParmDecl>(D)) {
13071314
if (NamingStyles[SK_TypeTemplateParameter])
13081315
return SK_TypeTemplateParameter;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,12 @@ Changes in existing checks
473473
it easier to see which specific enumerators need explicit initialization.
474474

475475
- Improved :doc:`readability-identifier-naming
476-
<clang-tidy/checks/readability/identifier-naming>` check by fixing incorrect
477-
naming style application to C++17 structured bindings.
476+
<clang-tidy/checks/readability/identifier-naming>` check:
477+
478+
- Fixed incorrect naming style application to C++17 structured bindings.
479+
480+
- Fixed a false positive where function templates could be diagnosed as generic
481+
identifiers when `DefaultCase` was enabled.
478482

479483
- Improved :doc:`readability-implicit-bool-conversion
480484
<clang-tidy/checks/readability/implicit-bool-conversion>` check:
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// RUN: %check_clang_tidy %s readability-identifier-naming %t -std=c++20-or-later \
2+
// RUN: --config='{CheckOptions: { \
3+
// RUN: readability-identifier-naming.DefaultCase: lower_case, \
4+
// RUN: readability-identifier-naming.ClassCase: CamelCase, \
5+
// RUN: readability-identifier-naming.FunctionCase: camelBack, \
6+
// RUN: readability-identifier-naming.GlobalVariableCase: camelBack, \
7+
// RUN: readability-identifier-naming.MethodCase: camelBack, \
8+
// RUN: readability-identifier-naming.TypeAliasCase: lower_case, \
9+
// RUN: readability-identifier-naming.TypeAliasSuffix: _t, \
10+
// RUN: }}'
11+
12+
#define BadMacro
13+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for identifier 'BadMacro' [readability-identifier-naming]
14+
// CHECK-FIXES: #define bad_macro
15+
16+
class Foo {
17+
public:
18+
template <typename t>
19+
void doStuff() {}
20+
21+
template <typename t>
22+
void DoStuff() {}
23+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for method 'DoStuff' [readability-identifier-naming]
24+
// CHECK-FIXES: void doStuff() {}
25+
};
26+
27+
template <typename t>
28+
void freeFunction() {}
29+
30+
template <typename t>
31+
void FreeFunction() {}
32+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: invalid case style for function 'FreeFunction' [readability-identifier-naming]
33+
// CHECK-FIXES: void freeFunction() {}
34+
35+
template <typename t>
36+
class GoodClass {};
37+
38+
template <typename t>
39+
class badClass {};
40+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'badClass' [readability-identifier-naming]
41+
// CHECK-FIXES: class BadClass {};
42+
43+
template <typename t>
44+
int goodVariable = 0;
45+
46+
template <typename t>
47+
int BadVariable = 0;
48+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for global variable 'BadVariable' [readability-identifier-naming]
49+
// CHECK-FIXES: int badVariable = 0;
50+
51+
template <typename t>
52+
using good_alias_t = int;
53+
54+
template <typename t>
55+
using BadAlias = int;
56+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for type alias 'BadAlias' [readability-identifier-naming]
57+
// CHECK-FIXES: using bad_alias_t = int;
58+
59+
int BadGlobal = 0;
60+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for global variable 'BadGlobal' [readability-identifier-naming]
61+
// CHECK-FIXES: int badGlobal = 0;

0 commit comments

Comments
 (0)