Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.UMINUS.UNSIGNED

Operand of unary minus is unsigned.

MISRA-C Rule 12.9 (required): The unary minus operator shall not be applied to an expression whose underlying type is unsigned.

Applying the unary minus operator to an expression of type unsigned int or unsigned long generates a result of type unsigned int or unsigned long respectively and is not a meaningful operation. Applying unary minus to an operand of smaller unsigned integer type may generate a meaningful signed result due to integral promotion, but this is not good practice.

See section 6.10 for a description of underlying type.

MISRA-C++ Rule 5-3-2 (required): The unary minus operator shall not be applied to an expression whose underlying type is unsigned.

Rationale

Applying the unary minus operator to an expression of type unsigned int, unsigned long or unsigned long long generates a result of type unsigned int, unsigned long or unsigned long long respectively and is not a meaningful operation. Applying unary minus to an operand of smaller unsigned integer type may generate a meaningful signed result due to integral promotion, but this is not considered good practice.

Example

On a machine with a 32-bit int type:

   uint8_t  a = -1U;  // Non-compliant — a is assigned 255
   int32_t  b = -a;   // Non-compliant — b is assigned -255
   uint32_t c = 1U;
   int64_t  d = -c;   // Non-compliant — d is assigned MAX_UINT