AUTOSAR C++14 Rule M5-2-3
Casts from a base class to a derived class should not be performed on polymorphic types
Description
Rule Definition
Casts from a base class to a derived class should not be performed on polymorphic types.
Rationale
Explicitly casting a polymorphic base class into a derived class bypasses the layers of abstraction in the implementation hierarchy, resulting in increased coupling and dependency between the classes.
When using a polymorphic class, avoid performing explicit downcasts. A pointer to the polymorphic base class does not require explicit casting into a pointer to a derived class.
Polyspace Implementation
Polyspace® raises a violation if a base class is cast to a derived class.
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: Expressions |
Category: Advisory, Automated |
Version History
Introduced in R2019a