Redundant expression in sizeof operand
Description
This defect occurs when a sizeof
operand contains expressions whose
evaluation does not affect the sizeof
result. In place of the current
expression in the sizeof
operand, a data type, a variable or a simpler
expression could have been used without any loss of functionality.
Risk
In situations flagged by this defect, the expression in the sizeof
operand is needlessly complicated, reduces the code readability and adds to maintainability
costs. The expression might also give a false impression about the result of the
sizeof
operand.
For instance, consider the expression:
sizeof(void (*[n])(int arr[U+V]))
sizeof
is an array of n
function pointers, each of
type void () (int*)
. The additional U+V
, which is not
evaluated, makes the full expression needlessly complicated. The expression also gives the
false impression that the function pointer argument being an array of size
U+V
matters for the sizeof
result.Fix
The first event in the defect traceback shows where the redundant subexpression of the
sizeof
operand begins.
Simplify or completely remove the redundant expression. When possible, use a data type
as the sizeof
operand. For instance, in the preceding example, a simpler
equivalent sizeof
operation
is:
sizeof(void (*[n])(int*))
If you want the expression to be evaluated, perform the evaluation in a separate statement.
Examples
Result Information
Group: Good practice |
Language: C | C++ |
Default: Off |
Command-Line Syntax:
SIZEOF_USELESS_OP |
Impact: Low |
Version History
Introduced in R2020a
See Also
Find defects
(-checkers)
| Side effect of expression
ignored
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)