MISRA C++:2023 Rule 22.3.1
Description
Rule Definition
The assert macro shall not be used with a constant-expression. 1
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
assertexpression 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
assertmacro can be disabled by using theNDEBUGflag or by other means. If the macro is disabled, your code bypasses checks using theassertmacro 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
falsecoupled 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
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.