MISRA C:2023 Rule 10.1
Description
Rule Definition
Operands shall not be of an inappropriate essential type.
Rationale
An essential type category defines the essential type of an object or expression.
Essential type category | Standard types |
---|---|
Essentially Boolean |
You can also define types that are
essentially Boolean using the option |
Essentially character |
char
|
Essentially enum | named enum |
Essentially signed | signed char , signed short ,
signed int , signed long , signed long
long |
Essentially unsigned | unsigned char , unsigned short ,
unsigned int , unsigned long ,
unsigned long long |
Essentially floating |
real floating: complex floating: |
For operands of some operators, you cannot use certain essential types. In the table below, each row represents an operator/operand combination. If the essential type column is not empty for that row, there is a MISRA restriction when using that type as the operand. The number in the table corresponds to the rationale list after the table.
Operation | Essential type category of arithmetic operand | |||||||
---|---|---|---|---|---|---|---|---|
Operator | Operand | Boolean | character | enum | signed | unsigned | floating | |
real | complex | |||||||
[ ]
| integer | 3 | 4 | 1 | 9 | |||
+ (unary) | 3 | 4 | 5 | |||||
- (unary) | 3 | 4 | 5 | 8 | ||||
+ -
| either | 3 | 5 | |||||
++ --
| 3 | 5 | 9 | |||||
* /
| either | 3 | 4 | 5 | ||||
%
| either | 3 | 4 | 5 | 1 | 9 | ||
< > <= >=
| either | 3 | 9 | |||||
== !=
| either | 10 | 10 | |||||
! && ||
| any | 2 | 2 | 2 | 2 | 2 | 2 | |
<< >>
| left | 3 | 4 | 5,6 | 6 | 1 | 9 | |
<< >>
| right | 3 | 4 | 7 | 7 | 1 | 9 | |
~ & | ^
| any | 3 | 4 | 5,6 | 6 | 1 | 9 | |
?:
| 1st | 2 | 2 | 2 | 2 | 2 | 2 | |
?:
| 2nd and 3rd |
An expression of essentially floating type for these operands is a constraint violation.
When an operand is interpreted as a Boolean value, use an expression of essentially Boolean type.
When an operand is interpreted as a numeric value, do not use an operand of essentially Boolean type.
When an operand is interpreted as a numeric value, do not use an operand of essentially character type. The numeric values of character data are implementation-defined.
In an arithmetic operation, do not use an operand of essentially enum type. An enum object uses an implementation-defined integer type. An operation involving an enum object can therefore yield a result with an unexpected type.
Perform only shift and bitwise operations on operands of essentially unsigned type. When you use shift and bitwise operations on essentially signed types, the resulting numeric value is undefined or implementation-defined.
To avoid undefined behavior on negative shifts, use an essentially unsigned right-hand operand.
For the unary minus operator, do not use an operand of essentially unsigned type. The implemented size of int determines the signedness of the result.
An expression of essentially complex floating type for these operands is a constraint violation.
Comparing floating-point types can result in unexpected and implementation-dependent results. When comparing floating-point types, take into account the floating point granularity (
FLT_EPSILON
) and the magnitudes of the numbers.
Note that for a bit-field type, if the bit-field is implemented as:
A Boolean, the bit-field is essentially Boolean.
Signed or unsigned type, the bit-field is essentially signed or unsigned respectively.
The type of the bit-field is the smallest type that can represent the bit-field. For instance, the type
stmp
here is essentially 8 bits integer:typedef signed int mybitfield; typedef struct { mybitfield f1 : 1; } stmp;
Polyspace Implementation
Polyspace® reports violation of this rule when the operand of an operation is of inappropriate essential types. As exceptions, Polyspace does not report violation on these use cases:
Using a non-negative integer constant expression of essentially signed type as the right operand of a shift operator.
Comparing essentially real floating or essentially complex floating object with the constant literal zero or the macro
INFINITY
or-INFINITY
.
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: The Essential Type Model |
Category: Required |
AGC Category: Advisory |
Version History
Introduced in R2024a