MISRAC++2008-5-3-2
In this section:
Synopsis
(Required) The unary minus operator shall not be applied to an expression whose underlying type is unsigned.
Enabled by default
Yes
Severity/Certainty
Low/Medium

Full description
Uses of unary minus on unsigned expressions were found. This check is identical to MISRAC2012-Rule-10.1_R8, MISRAC2004-12.9, MISRAC++2023-8.3.1.
Coding standards
- MISRA C:2004 12.9
(Required) The unary minus operator shall not be applied to an expression whose underlying type is unsigned.
- MISRA C:2012 Rule-10.1
(Required) Operands shall not be of an inappropriate essential type
- MISRA C++ 2023 8.3.1
(Advisory) The built-in unary - operator should not be applied to an expression of unsigned type
Code examples
The following code example fails the check and will give a warning:
void example(void) {
unsigned int max = -1U;
// use max = ~0U;
}
The following code example passes the check and will not give a warning about this issue:
void example(void) {
int neg_one = -1;
}