Unmodified variable not const-qualified
Variable not const
-qualified but variable value not modified
during lifetime
Description
This defect occurs when a local variable or function parameter is not const
-qualified and one of the following statements is true during the variable or parameter lifetime:
You do not write to the variable after initialization, or write to the parameter anywhere in the function body.
When you perform write operations, you reassign the same constant value to the variable or parameter.
The checker considers a variable as modified if its address is assigned to a pointer or reference (unless it is a pointer or reference to a const
variable), passed to another function, or otherwise used. In these situations, the checker does not suggest adding a const
qualifier.
The checker flags arrays as candidates for const
-qualification only if
you do not perform write operations on the array elements at all after initialization.
Risk
const
-qualifying a variable avoids unintended modification of the
variable during later code maintenance. The const
qualifier also
indicates to a developer that the variable retains its initial value in the remainder of the
code.
Fix
If you do not expect to modify a variable value during its lifetime, add the
const
qualifier to the variable declaration and initialize the variable
at declaration.
If you expect the variable to be modified, see if the absence of a modification indicates a programming omission and fix the issue.
Examples
Result Information
Group: Good practice |
Language: C | C++ |
Default: Off |
Command-Line Syntax:
UNMODIFIED_VAR_NOT_CONST |
Impact: Low |
Version History
Introduced in R2020aSee Also
Find defects
(-checkers)
| Expensive local variable
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)