13
13
14
14
using namespace clang ::tidy::readability;
15
15
16
- static std::optional<std::string>
17
- generateCaptureText (const clang::LambdaCapture &Capture) {
16
+ static std::string generateCaptureText (const clang::LambdaCapture &Capture) {
18
17
if (Capture.capturesThis ()) {
19
18
return Capture.getCaptureKind () == clang::LCK_StarThis ? " *this" : " this" ;
20
19
}
21
20
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 += " &" ;
29
24
}
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;
38
27
}
39
28
40
29
void AvoidDefaultLambdaCaptureCheck::registerMatchers (
@@ -59,24 +48,16 @@ void AvoidDefaultLambdaCaptureCheck::check(
59
48
" lambda default captures are discouraged; "
60
49
" prefer to capture specific variables explicitly" );
61
50
62
- std::vector<std::string> AllCaptures ;
51
+ std::vector<std::string> ImplicitCaptures ;
63
52
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));
68
55
}
69
56
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, " , " );
75
59
}();
76
60
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);
82
63
}
0 commit comments