-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Add readability-avoid-default-lambda-capture #160150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jjmarr-amd
wants to merge
38
commits into
llvm:main
Choose a base branch
from
jjmarr-amd:DefaultLambdaCapture
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+328
−0
Open
Changes from 3 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
35df1c6
Bugprone-default-lambda-capture
jjmarr-amd dbb903f
Merge branch 'main' of github.com:llvm/llvm-project into DefaultLambd…
jjmarr-amd 689f95b
Fix headers
jjmarr-amd 9991b9c
remove superfluous comments.
jjmarr-amd db17a4c
add new matcher
jjmarr-amd 7dcf216
Replace if with assert
jjmarr-amd 176d195
Remove specific warning type
jjmarr-amd 5f23dff
Sync documentation to release notes
jjmarr-amd 4c41332
Update clang-tools-extra/clang-tidy/bugprone/DefaultLambdaCaptureCheck.h
jjmarr-amd 330dbd8
Remove superfluous include
jjmarr-amd 31366e6
Fix doc and release notes
jjmarr-amd 70df9df
Fix const
jjmarr-amd 0418517
Merge branch 'main' into DefaultLambdaCapture
jjmarr-amd 0bcdbc4
Fix header guard
jjmarr-amd ac709f4
Fix header spelling
jjmarr-amd 5d4c2ca
Rename check to readability-default-lambda-capture
jjmarr-amd c0b90d1
Update clang-tools-extra/docs/clang-tidy/checks/readability/default-l…
jjmarr-amd 1172f73
update doc
jjmarr-amd 66e6aaa
rename check to readability-avoid-default-lambda-capture
jjmarr-amd 4bf58b6
use C++ style comments
jjmarr-amd a807dd0
Update clang-tools-extra/docs/clang-tidy/checks/readability/avoid-def…
jjmarr-amd 8fdc097
fix doc again
jjmarr-amd ab8f4b9
remove reference to coding guideline
jjmarr-amd a4348de
Merge branch 'main' of github.com:llvm/llvm-project into DefaultLambd…
jjmarr-amd 13b5049
rephrase
jjmarr-amd 043df7c
commit vibe coded work
jjmarr-amd 33af58b
attempt to fix to use something that isn't weird string manipulation
jjmarr-amd 51aa182
more vibe coding with human intervention
jjmarr-amd dd65b90
go back to the hacky string manipulation
jjmarr-amd a9509f3
remove AI comments
jjmarr-amd e9e0f9a
fix namespacing issues
jjmarr-amd b2c3cc2
remove anonymous namespace
jjmarr-amd 969558e
simplify per code review
jjmarr-amd 359fd6c
Use IIFE so ReplacementText can be const.
jjmarr-amd 4e10e78
simplify code even more
jjmarr-amd 9765c90
Turns out this check doesn't like templated lambdas
jjmarr-amd 33a79a0
ignore VLAs
jjmarr-amd 8f21201
move around doc paragraphs
jjmarr-amd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...tools-extra/docs/clang-tidy/checks/readability/avoid-default-lambda-capture.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
.. title:: clang-tidy - readability-avoid-default-lambda-capture | ||
|
||
readability-avoid-default-lambda-capture | ||
========================================= | ||
jjmarr-amd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Warns on default lambda captures (e.g. ``[&](){ ... }``, ``[=](){ ... }``) | ||
jjmarr-amd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Captures can lead to subtle bugs including dangling references and unnecessary | ||
copies. Writing out the name of the variables being captured reminds programmers | ||
and reviewers to know what is being captured. And knowing is half the battle. | ||
|
||
Coding guidelines that recommend against defaulted lambda captures include: | ||
|
||
* Item 31 of Effective Modern C++ by Scott Meyers | ||
* `AUTOSAR C++ Rule A5-1-2 <https://www.mathworks.com/help//releases/ | ||
|
||
R2021a/bugfinder/ref/autosarc14rulea512.html>`__ | ||
|
||
Example | ||
------- | ||
|
||
.. code-block:: c++ | ||
|
||
#include <iostream> | ||
|
||
class Widget { | ||
std::vector<std::function<void(int)>> callbacks; | ||
int widgetId; | ||
void addCallback(int factoryId) { | ||
callbacks.emplace_back( | ||
[&](){ | ||
std::cout << "Widget " << widgetId << " made in factory " << factoryId; | ||
} | ||
); | ||
} | ||
} | ||
|
||
When ``callbacks`` is executed, ``factoryId`` will dangle. Writing the name of | ||
``factoryId`` in the capture list makes it easy to review the captures and | ||
jjmarr-amd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
detect obvious bugs. | ||
|
||
.. code-block:: c++ | ||
|
||
#include <iostream> | ||
|
||
class Widget { | ||
std::vector<std::function<void(int)>> callbacks; | ||
int widgetId; | ||
void addCallback(int factoryId) { | ||
callbacks.emplace_back( | ||
[&factoryId, &widgetId](){ // Why isn't factoryId captured by value?? | ||
std::cout << "Widget " << widgetId << " made in factory " << factoryId; | ||
} | ||
); | ||
} | ||
} |
43 changes: 0 additions & 43 deletions
43
clang-tools-extra/docs/clang-tidy/checks/readability/default-lambda-capture.rst
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.