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.C_CAST

C-style cast to non-void type.

MISRA-C++ Rule 5-2-4 (required): C-style casts (other than void casts) and functional notation casts (other than explicit constructor calls) shall not be used.

This rule is also covered by MISRA.FUNC CAST.

Rationale

C-style (cast notation), and functional notation casts that do not invoke a converting constructor are capable of performing casts between unrelated types.

Exception

A C-style cast to void may be used to signify that the return value for a non-void function call is being ignored (see Rule 0—1—7).

Example

class A
{
public:
   explicit A( int32_t );
};

int32_t g ( )
{
   return 7;
}

void f ( )
{
   A const a1 = A( 10 );             // Compliant
   A * a2 = ( A* )( &a1 );           // Non-compliant
   A * a3 = const_cast<A*>( &a1 );   // Compliant, but breaks Rule 5—2—5
    (void)g ( );                     // Compliant by exception
}

In the above example, the C-style cast from 'a1' to a non-const pointer is stronger than necessary. If the type of 'a1' is changed at some future date, then the cast may continue to compile.