Skip to content

Commit 7fcca11

Browse files
authored
[clang-tidy] bugprone-lambda-function-name ignore macro in captures (#89076)
1 parent 6a35ee8 commit 7fcca11

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
#include "LambdaFunctionNameCheck.h"
1010
#include "clang/AST/ASTContext.h"
11+
#include "clang/AST/DeclCXX.h"
1112
#include "clang/ASTMatchers/ASTMatchFinder.h"
13+
#include "clang/ASTMatchers/ASTMatchers.h"
1214
#include "clang/Frontend/CompilerInstance.h"
1315
#include "clang/Lex/MacroInfo.h"
1416
#include "clang/Lex/Preprocessor.h"
@@ -56,6 +58,8 @@ class MacroExpansionsWithFileAndLine : public PPCallbacks {
5658
LambdaFunctionNameCheck::SourceRangeSet* SuppressMacroExpansions;
5759
};
5860

61+
AST_MATCHER(CXXMethodDecl, isInLambda) { return Node.getParent()->isLambda(); }
62+
5963
} // namespace
6064

6165
LambdaFunctionNameCheck::LambdaFunctionNameCheck(StringRef Name,
@@ -69,9 +73,13 @@ void LambdaFunctionNameCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
6973
}
7074

7175
void LambdaFunctionNameCheck::registerMatchers(MatchFinder *Finder) {
72-
// Match on PredefinedExprs inside a lambda.
73-
Finder->addMatcher(predefinedExpr(hasAncestor(lambdaExpr())).bind("E"),
74-
this);
76+
Finder->addMatcher(
77+
cxxMethodDecl(isInLambda(),
78+
hasBody(forEachDescendant(
79+
predefinedExpr(hasAncestor(cxxMethodDecl().bind("fn")))
80+
.bind("E"))),
81+
equalsBoundNode("fn")),
82+
this);
7583
}
7684

7785
void LambdaFunctionNameCheck::registerPPCallbacks(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ Changes in existing checks
155155
<clang-tidy/checks/bugprone/inc-dec-in-conditions>` check to ignore code
156156
within unevaluated contexts, such as ``decltype``.
157157

158+
- Improved :doc:`bugprone-lambda-function-name<clang-tidy/checks/bugprone/lambda-function-name>`
159+
check by ignoring ``__func__`` macro in lambda captures, initializers of
160+
default parameters and nested function declarations.
161+
158162
- Improved :doc:`bugprone-non-zero-enum-to-bool-conversion
159163
<clang-tidy/checks/bugprone/non-zero-enum-to-bool-conversion>` check by
160164
eliminating false positives resulting from direct usage of bitwise operators

clang-tools-extra/test/clang-tidy/checkers/bugprone/lambda-function-name.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ void Positives() {
1919
// CHECK-MESSAGES-NO-CONFIG: :[[@LINE-1]]:8: warning: inside a lambda, '__FUNCTION__' expands to the name of the function call operator; consider capturing the name of the enclosing function explicitly [bugprone-lambda-function-name]
2020
[] { EMBED_IN_ANOTHER_MACRO1; }();
2121
// CHECK-MESSAGES-NO-CONFIG: :[[@LINE-1]]:8: warning: inside a lambda, '__func__' expands to the name of the function call operator; consider capturing the name of the enclosing function explicitly [bugprone-lambda-function-name]
22+
[] {
23+
__func__;
24+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: inside a lambda, '__func__' expands to the name of the function call operator; consider capturing the name of the enclosing function explicitly [bugprone-lambda-function-name]
25+
struct S {
26+
void f() {
27+
__func__;
28+
[] {
29+
__func__;
30+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: inside a lambda, '__func__' expands to the name of the function call operator; consider capturing the name of the enclosing function explicitly [bugprone-lambda-function-name]
31+
}();
32+
__func__;
33+
}
34+
};
35+
__func__;
36+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: inside a lambda, '__func__' expands to the name of the function call operator; consider capturing the name of the enclosing function explicitly [bugprone-lambda-function-name]
37+
}();
2238
}
2339

2440
#define FUNC_MACRO_WITH_FILE_AND_LINE Foo(__func__, __FILE__, __LINE__)
@@ -40,4 +56,7 @@ void Negatives() {
4056
[] { FUNC_MACRO_WITH_FILE_AND_LINE; }();
4157
[] { FUNCTION_MACRO_WITH_FILE_AND_LINE; }();
4258
[] { EMBED_IN_ANOTHER_MACRO2; }();
59+
60+
[] (const char* func = __func__) { func; }();
61+
[func=__func__] { func; }();
4362
}

0 commit comments

Comments
 (0)