Skip to content

Commit 4e10e78

Browse files
committed
simplify code even more
on-behalf-of: @amd <[email protected]>
1 parent 359fd6c commit 4e10e78

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

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

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,17 @@
1313

1414
using namespace clang::tidy::readability;
1515

16-
static std::optional<std::string>
17-
generateCaptureText(const clang::LambdaCapture &Capture) {
16+
static std::string generateCaptureText(const clang::LambdaCapture &Capture) {
1817
if (Capture.capturesThis()) {
1918
return Capture.getCaptureKind() == clang::LCK_StarThis ? "*this" : "this";
2019
}
2120

22-
if (Capture.capturesVariable()) {
23-
std::string Result;
24-
if (Capture.getCaptureKind() == clang::LCK_ByRef) {
25-
Result += "&";
26-
}
27-
Result += Capture.getCapturedVar()->getName().str();
28-
return Result;
21+
std::string Result;
22+
if (Capture.getCaptureKind() == clang::LCK_ByRef) {
23+
Result += "&";
2924
}
30-
31-
if (Capture.capturesVLAType()) {
32-
// VLA captures are rare and complex - for now we skip them
33-
// A full implementation would need to handle the VLA type properly
34-
return std::nullopt;
35-
}
36-
37-
return std::nullopt;
25+
Result += Capture.getCapturedVar()->getName().str();
26+
return Result;
3827
}
3928

4029
void AvoidDefaultLambdaCaptureCheck::registerMatchers(
@@ -59,24 +48,16 @@ void AvoidDefaultLambdaCaptureCheck::check(
5948
"lambda default captures are discouraged; "
6049
"prefer to capture specific variables explicitly");
6150

62-
std::vector<std::string> AllCaptures;
51+
std::vector<std::string> ImplicitCaptures;
6352

64-
for (const auto &Capture : Lambda->captures()) {
65-
if (const auto CaptureText = generateCaptureText(Capture)) {
66-
AllCaptures.push_back(CaptureText.value());
67-
}
53+
for (const auto &Capture : Lambda->implicit_captures()) {
54+
ImplicitCaptures.push_back(generateCaptureText(Capture));
6855
}
6956

70-
const auto ReplacementText = [&AllCaptures]() -> std::string {
71-
if (AllCaptures.empty()) {
72-
return "[]";
73-
}
74-
return "[" + llvm::join(AllCaptures, ", ") + "]";
57+
const auto ReplacementText = [&ImplicitCaptures]() {
58+
return llvm::join(ImplicitCaptures, ", ");
7559
}();
7660

77-
const clang::SourceRange IntroducerRange = Lambda->getIntroducerRange();
78-
if (IntroducerRange.isValid()) {
79-
Diag << clang::FixItHint::CreateReplacement(IntroducerRange,
80-
ReplacementText);
81-
}
61+
Diag << clang::FixItHint::CreateReplacement(Lambda->getCaptureDefaultLoc(),
62+
ReplacementText);
8263
}

clang-tools-extra/test/clang-tidy/checkers/readability/avoid-default-lambda-capture.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ void test_default_captures() {
1414

1515
auto lambda3 = [=, &another](int x) { return value + another + x; };
1616
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: lambda default captures are discouraged; prefer to capture specific variables explicitly [readability-avoid-default-lambda-capture]
17-
// CHECK-FIXES: auto lambda3 = [&another, value](int x) { return value + another + x; };
17+
// CHECK-FIXES: auto lambda3 = [value, &another](int x) { return value + another + x; };
1818

1919
auto lambda4 = [&, value](int x) { return value + another + x; };
2020
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: lambda default captures are discouraged; prefer to capture specific variables explicitly [readability-avoid-default-lambda-capture]
21-
// CHECK-FIXES: auto lambda4 = [value, &another](int x) { return value + another + x; };
21+
// CHECK-FIXES: auto lambda4 = [&another, value](int x) { return value + another + x; };
2222
}
2323

2424
void test_acceptable_captures() {
@@ -101,10 +101,31 @@ void test_template_lambdas() {
101101

102102
auto lambda = [=](T x) { return value + x; };
103103
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: lambda default captures are discouraged; prefer to capture specific variables explicitly [readability-avoid-default-lambda-capture]
104-
// CHECK-FIXES: auto lambda = [](T x) { return value + x; };
104+
// CHECK-FIXES: auto lambda = [value](T x) { return value + x; };
105105
}
106106

107107
void instantiate_templates() {
108108
test_template_lambdas<int>();
109109
test_template_lambdas<double>();
110110
}
111+
112+
void test_init_captures() {
113+
int x = 3;
114+
int nx = 5;
115+
116+
int y1 = [&, z = x + 5]() -> int {
117+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: lambda default captures are discouraged; prefer to capture specific variables explicitly [readability-avoid-default-lambda-capture]
118+
// CHECK-FIXES: int y1 = [&nx, z = x + 5]() -> int {
119+
return z * z + nx;
120+
}();
121+
122+
int y2 = [=, &ref = x]() {
123+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: lambda default captures are discouraged; prefer to capture specific variables explicitly [readability-avoid-default-lambda-capture]
124+
// CHECK-FIXES: int y2 = [nx, &ref = x]() {
125+
ref += 1;
126+
return nx - ref;
127+
}();
128+
129+
(void)y1;
130+
(void)y2;
131+
}

0 commit comments

Comments
 (0)