Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
35df1c6
Bugprone-default-lambda-capture
jjmarr-amd Jul 11, 2025
dbb903f
Merge branch 'main' of github.com:llvm/llvm-project into DefaultLambd…
jjmarr-amd Sep 22, 2025
689f95b
Fix headers
jjmarr-amd Sep 22, 2025
9991b9c
remove superfluous comments.
jjmarr-amd Sep 22, 2025
db17a4c
add new matcher
jjmarr-amd Sep 22, 2025
7dcf216
Replace if with assert
jjmarr-amd Sep 22, 2025
176d195
Remove specific warning type
jjmarr-amd Sep 22, 2025
5f23dff
Sync documentation to release notes
jjmarr-amd Sep 22, 2025
4c41332
Update clang-tools-extra/clang-tidy/bugprone/DefaultLambdaCaptureCheck.h
jjmarr-amd Sep 22, 2025
330dbd8
Remove superfluous include
jjmarr-amd Sep 22, 2025
31366e6
Fix doc and release notes
jjmarr-amd Sep 22, 2025
70df9df
Fix const
jjmarr-amd Sep 22, 2025
0418517
Merge branch 'main' into DefaultLambdaCapture
jjmarr-amd Sep 22, 2025
0bcdbc4
Fix header guard
jjmarr-amd Sep 23, 2025
ac709f4
Fix header spelling
jjmarr-amd Sep 23, 2025
5d4c2ca
Rename check to readability-default-lambda-capture
jjmarr-amd Sep 24, 2025
c0b90d1
Update clang-tools-extra/docs/clang-tidy/checks/readability/default-l…
jjmarr-amd Sep 24, 2025
1172f73
update doc
jjmarr-amd Sep 25, 2025
66e6aaa
rename check to readability-avoid-default-lambda-capture
jjmarr-amd Sep 25, 2025
4bf58b6
use C++ style comments
jjmarr-amd Sep 25, 2025
a807dd0
Update clang-tools-extra/docs/clang-tidy/checks/readability/avoid-def…
jjmarr-amd Sep 25, 2025
8fdc097
fix doc again
jjmarr-amd Sep 25, 2025
ab8f4b9
remove reference to coding guideline
jjmarr-amd Sep 25, 2025
a4348de
Merge branch 'main' of github.com:llvm/llvm-project into DefaultLambd…
jjmarr-amd Sep 29, 2025
13b5049
rephrase
jjmarr-amd Sep 29, 2025
043df7c
commit vibe coded work
jjmarr-amd Sep 29, 2025
33af58b
attempt to fix to use something that isn't weird string manipulation
jjmarr-amd Sep 29, 2025
51aa182
more vibe coding with human intervention
jjmarr-amd Sep 29, 2025
dd65b90
go back to the hacky string manipulation
jjmarr-amd Sep 29, 2025
a9509f3
remove AI comments
jjmarr-amd Sep 29, 2025
e9e0f9a
fix namespacing issues
jjmarr-amd Sep 29, 2025
b2c3cc2
remove anonymous namespace
jjmarr-amd Sep 29, 2025
969558e
simplify per code review
jjmarr-amd Oct 1, 2025
359fd6c
Use IIFE so ReplacementText can be const.
jjmarr-amd Oct 1, 2025
4e10e78
simplify code even more
jjmarr-amd Oct 3, 2025
9765c90
Turns out this check doesn't like templated lambdas
jjmarr-amd Oct 3, 2025
33a79a0
ignore VLAs
jjmarr-amd Oct 8, 2025
8f21201
move around doc paragraphs
jjmarr-amd Oct 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,19 @@ void AvoidDefaultLambdaCaptureCheck::check(
if (DefaultCaptureLoc.isInvalid())
return;

auto Diag = diag(DefaultCaptureLoc,
"lambda default captures are discouraged; "
"prefer to capture specific variables explicitly");

std::vector<std::string> ImplicitCaptures;

for (const auto &Capture : Lambda->implicit_captures()) {
// It is impossible to explicitly capture a VLA in C++, since VLAs don't
// exist in ISO C++ and so the syntax was never created to capture them.
if (Capture.getCaptureKind() == LCK_VLAType)
return;
ImplicitCaptures.push_back(generateCaptureText(Capture));
}

auto Diag = diag(DefaultCaptureLoc,
"lambda default captures are discouraged; "
"prefer to capture specific variables explicitly");

// For template-dependent lambdas, the list of captures hasn't been created
// yet, so the list of implicit captures is empty.
if (ImplicitCaptures.empty() && Lambda->isGenericLambda())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Coding guidelines that recommend against defaulted lambda captures include:

* Item 31 of Effective Modern C++ by Scott Meyers

This check does not lint for variable-length array (VLA) captures. VLAs are not
ISO C++, and it is impossible to explicitly capture them as the syntax does not
exist.

Example
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy %s readability-avoid-default-lambda-capture %t
// RUN: %check_clang_tidy %s readability-avoid-default-lambda-capture %t -- -- -Wno-vla-extension

void test_default_captures() {
int value = 42;
Expand Down Expand Up @@ -129,3 +129,15 @@ void test_init_captures() {
(void)y1;
(void)y2;
}

void test_vla_no_crash() {
// VLAs create implicit VLA bound captures that cannot be written explicitly.
// No warning should be issued.
int n = 5;
int vla[n];
for (int i = 0; i < n; ++i) {
vla[i] = i * 10;
}

auto lambda = [&]() { return vla[0]; };
}
Loading