MISRA C++:2008 Rule 6-4-2
All if … else if constructs shall be terminated with an else clause
Description
Rule Definition
All if ... else if constructs shall be terminated with an else clause.1
Rationale
Ending an if... else if construct with an else statement is defensive
programming. This final else statement acts as a fail-safe in case a
unique situation occurs where the code progresses past the if and
else if statements.
When an if statement is followed by one or more else
if statements, follow the final else if statement with an
else statement. Within the else statement provide an
action. If no action is needed, provide a comment as to why no action is taken.
Polyspace Implementation
Polyspace® raises this defect whenever an if … else if construct does
not end with an else statement.
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: Statements |
| Category: Required |
Version History
Introduced in R2013b
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.