|
| 1 | +/** |
| 2 | + * @id c/misra/addition-subtraction-on-essentially-char-type |
| 3 | + * @name RULE-10-2: Expressions of essentially character type shall not be used inappropriately in addition and |
| 4 | + * @description Expressions of essentially character type shall not be used inappropriately in |
| 5 | + * addition and subtraction operations |
| 6 | + * @kind problem |
| 7 | + * @precision very-high |
| 8 | + * @problem.severity error |
| 9 | + * @tags external/misra/id/rule-10-2 |
| 10 | + * external/misra/obligation/required |
| 11 | + */ |
| 12 | + |
| 13 | +import cpp |
| 14 | +import codingstandards.c.misra |
| 15 | +import codingstandards.c.misra.EssentialTypes |
| 16 | + |
| 17 | +from BinaryArithmeticOperation addOrSub |
| 18 | +where |
| 19 | + not isExcluded(addOrSub, EssentialTypesPackage::additionSubtractionOnEssentiallyCharTypeQuery()) and |
| 20 | + addOrSub.getOperator() = ["+", "-"] and |
| 21 | + // At least one operand is essentially character type |
| 22 | + ( |
| 23 | + getEssentialTypeCategory(getEssentialType(addOrSub.getLeftOperand())) = |
| 24 | + EssentiallyCharacterType() or |
| 25 | + getEssentialTypeCategory(getEssentialType(addOrSub.getRightOperand())) = |
| 26 | + EssentiallyCharacterType() |
| 27 | + ) and |
| 28 | + not ( |
| 29 | + // But the overall essential type is not essentially character type |
| 30 | + getEssentialTypeCategory(getEssentialType(addOrSub)) = EssentiallyCharacterType() |
| 31 | + or |
| 32 | + // Or this is a subtration of one character with another, which is permitted, but produces an integral type |
| 33 | + getEssentialTypeCategory(getEssentialType(addOrSub.getLeftOperand())) = |
| 34 | + EssentiallyCharacterType() and |
| 35 | + getEssentialTypeCategory(getEssentialType(addOrSub.getRightOperand())) = |
| 36 | + EssentiallyCharacterType() and |
| 37 | + addOrSub instanceof SubExpr |
| 38 | + ) |
| 39 | +select addOrSub, |
| 40 | + "Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations" |
0 commit comments