Expensive last use of variable
An expensive-to-copy local variable is copied in its final use instead of being moved
Since R2025a
Description
Polyspace® reports this defect when both of these conditions are true:
The final use of a local variable in its scope is a copy operation.
It is less expensive to move the variable than it is to copy it.
Risk
If a variable is expensive to copy, then copying the variable on its last use in its scope provides no benefit and makes the code inefficient.
Fix
If an expensive-to-copy object is not used later in its scope, use
std::move()
to move the object instead of copying it on its last
use.
Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
Examples
Result Information
Group: Performance |
Language: C++ |
Default: Off |
Command-Line Syntax:
EXPENSIVE_COPY_ON_LAST_USE
|
Impact: Medium |
Version History
Introduced in R2025a
See Also
Find defects
(-checkers)
| Move operation uses
copy
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)
- Expensive-to-Read Objects in Bug Finder