MISRA C++:2023 Rule 22.3.1
Description
Rule Definition
The assert macro shall not be used with a constant-expression.
Rationale
The assert
macro is meant for checking conditions that can be evaluated only at run time. To check the value of an expression that is evaluated at compile time, use the static_assert
macro instead.
The use of the assert
macro with constant expressions can be problematic for these reasons:
If the
assert
expression fails, the failure is shown only at run time. With compile-time constants, you might want a failure to be reported at compilation time.The
assert
macro can be disabled by using theNDEBUG
flag or by other means. If the macro is disabled, your code bypasses checks using theassert
macro and might show unexpected behavior.
Polyspace Implementation
The rule checker reports a violation on an assert
macro if the macro argument is a compile-time constant. Following the MISRA™ C++:2023 specifications, the checker allows an exception for these cases:
If the argument is the literal value
false
.If the argument is the literal value
false
coupled to a string literal with the&&
operator.
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: Diagnostics library |
Category: Required |
Version History
Introduced in R2024b