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.ETYPE.INAPPR.CAST.2012

The value of an expression is cast to an inappropriate essential type.

MISRA C 2012 Rule 10.5: The value of an expression should not be cast to an inappropriate essential type

Category: Advisory

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Amplification

The casts which should be avoided are shown in the following table, where values are cast (explicitly converted) to the essential type category of the first column.

Essential type category from
to Boolean character enum signed unsigned floating
Boolean   Avoid Avoid Avoid Avoid Avoid
character Avoid         Avoid
enum Avoid Avoid Avoid* Avoid Avoid Avoid
signed Avoid          
unsigned Avoid          
floating Avoid Avoid        
Note: * an enumerated type may be cast to an enumerated type provided that the cast is to the same essential enumerated type. Such casts are redundant.

Casting from void to any other type is not permitted as it results in undefined behaviour. This is covered by Rule 1.3.

Rationale

An explicit cast may be introduced for legitimate functional reasons, for example:
  • To change the type in which a subsequent arithmetic operation is performed;
  • To truncate a value deliberately;
  • To make a type conversion explicit in the interests of clarity.
However, some explicit casts are considered inappropriate:
  • In C99, the result of a cast or assignment to _Bool is always 0 or 1. This is not necessarily the case when casting to another type which is defined as essentially Boolean;
  • A cast to an essentially enum type may result in a value that does not lie within the set of enumeration constants for that type;
  • A cast from essentially Boolean to any other type is unlikely to be meaningful;
  • Converting between floating and character types is not meaningful as there is no precise mapping between the two representations.

Exception

An integer constant expression with the value 0 or 1 of either signedness may be cast to a type which is defined as essentially Boolean. This allows the implementation of non-C99 Boolean models.

Example

( bool_t ) false    /* Compliant - C99 'false' is essentially Boolean */
( int32_t ) 3U      /* Compliant                                      */
( bool_t ) 0        /* Compliant - by exception                       */
( bool_t ) 3U       /* Non-compliant                                  */

( int32_t ) ena     /* Compliant                                      */
( enum enuma ) 3    /* Non-compliant                                  */
( char ) enc        /* Compliant                                      */