|
| 1 | +/** |
| 2 | + * @id c/misra/inappropriate-cast-of-composite-expression |
| 3 | + * @name RULE-10-8: The value of a composite expression shall not be cast to a different essential type category or a |
| 4 | + * @description The value of a composite expression shall not be cast to a different essential type |
| 5 | + * category or a wider essential type |
| 6 | + * @kind problem |
| 7 | + * @precision very-high |
| 8 | + * @problem.severity error |
| 9 | + * @tags external/misra/id/rule-10-8 |
| 10 | + * external/misra/obligation/required |
| 11 | + */ |
| 12 | + |
| 13 | +import cpp |
| 14 | +import codingstandards.c.misra |
| 15 | +import codingstandards.c.misra.EssentialTypes |
| 16 | +import codingstandards.c.misra.MisraExpressions |
| 17 | + |
| 18 | +from |
| 19 | + Cast c, CompositeExpression ce, Type castEssentialType, Type compositeExprEssentialType, |
| 20 | + EssentialTypeCategory castTypeCategory, EssentialTypeCategory compositeTypeCategory, |
| 21 | + string message |
| 22 | +where |
| 23 | + not isExcluded(ce, EssentialTypesPackage::inappropriateCastOfCompositeExpressionQuery()) and |
| 24 | + c = ce.getExplicitlyConverted() and |
| 25 | + compositeExprEssentialType = getEssentialTypeBeforeConversions(ce) and |
| 26 | + castEssentialType = c.getType() and |
| 27 | + castTypeCategory = getEssentialTypeCategory(castEssentialType) and |
| 28 | + compositeTypeCategory = getEssentialTypeCategory(compositeExprEssentialType) and |
| 29 | + ( |
| 30 | + not castTypeCategory = compositeTypeCategory and |
| 31 | + message = |
| 32 | + "Cast from " + compositeTypeCategory + " to " + castTypeCategory + " changes type category." |
| 33 | + or |
| 34 | + castTypeCategory = compositeTypeCategory and |
| 35 | + castEssentialType.getSize() > compositeExprEssentialType.getSize() and |
| 36 | + message = "Cast from " + compositeTypeCategory + " to " + castTypeCategory + " widens type." |
| 37 | + ) |
| 38 | +select ce, message |
0 commit comments