Useless capture
Description
This defect occurs when a lambda expression captures objects either implicitly or explicitly, but does not use them. For example, the lambda L1
captures the objects of its scope implicitly by reference but uses none of them:
auto L1 = [&](){};
L1
.Risk
An unused captured object can indicate possible logic errors or architectural errors in your code. Useless captures prevent the lambda from being assigned to a function pointer. If an unused captured object is expensive to construct or destroy, then it can make the code unnecessarily expensive.
Fix
To fix this defect, review your code and determine why captured objects are unused. Remove useless captures from your lambda expressions.
Examples
Result Information
Group: Good Practice |
Language: C++ |
Default: Off |
Command-Line Syntax:
USELESS_CAPTURE
|
Impact: Low |
Version History
Introduced in R2024b
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)