MISRA C++:2008 Rule 0-1-4
A project shall not contain non-volatile POD variables having only one use
Since R2020b
Description
Rule Definition
A project shall not contain non-volatile POD variables having only one use.
Rationale
If you use a non-volatile variable with a Plain Old Data type (int
,
double
, etc.) only once, you can replace the
variable with a constant literal. Your use of a variable indicates that you intended more
than one use for that variable and might have a programming error in the code. You might
have omitted the other uses of the non-volatile variable or incorrectly used other variables
at intended points of use.
Polyspace Implementation
The checker flags local and static variables that have a function scope (locally static)
and file scope, which are used only once. The checker considers
const
-qualified global variables without the extern
specifier as static variables with file scope.
The checker counts these use cases as one use of the non-volatile variable:
An explicit initialization using a constant literal or the return value of a function
An assignment
A reference to the variable such as a read operation
An assignment of the variable address to a pointer
If the variable address is assigned to a pointer, the checker assumes that the pointer might be dereferenced later and does not flag the variable.
Some objects are designed to be used only once by their semantics. Polyspace® does not flag a single use of these objects:
lock_guard
scoped_lock
shared_lock
unique_lock
thread
future
shared_future
If you use nonstandard objects that provide similar functionality as the objects in the preceding list, Polyspace might flag single uses of the nonstandard objects. Justify their single uses by using comments.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Language Independent Issues |
Category: Required |