Skip to main content

IAR Embedded Workbench for Arm 9.70.x

MISRAC++2023-8.2.3

In this section:
Synopsis

(Required) A cast shall not remove any const or volatile qualification from the type accessed via a pointer or by reference

Enabled by default

Yes

Severity/Certainty

Low/High

lowhigh.png
Full description

Casts that remove a const or volatile qualification were found. This check is identical to MISRAC++2008-5-2-5, MISRAC2004-11.5, MISRAC2012-Rule-11.8.

Coding standards
MISRA C:2004 11.5

(Required) A cast shall not be performed that removes any const or volatile qualification from the type addressed by a pointer.

MISRA C:2012 Rule-11.8

(Required) A cast shall not remove any const or volatile qualification from the type pointed to by a pointer

MISRA C++ 2008 5-2-5

(Required) A cast shall not remove any const or volatile qualification from the type of a pointer or reference.

Code examples

The following code example fails the check and will give a warning:

typedef unsigned short uint16_t;

void example(void) {

	uint16_t x;
	const uint16_t *    pci;      /* pointer to const int */
	uint16_t *          pi;       /* pointer to int */

	pi = (uint16_t *)pci; // not compliant

}

The following code example passes the check and will not give a warning about this issue:

typedef unsigned short uint16_t;

void example(void) {

	uint16_t x;
	uint16_t * const    cpi = &x; /* const pointer to int */
	uint16_t *          pi;       /* pointer to int */

	pi = cpi; // compliant - no cast required

}