C-style cast in C++ code

ID

c.maintainability.c_style_cast

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

medium

Resource

Cast

Language

C / C++

Description

A C-style cast (T)expr silently performs whichever conversion compiles — including reinterpret- or const-casts — so the intent is unclear and dangerous conversions go unnoticed. Use a named cast (static_cast, const_cast, reinterpret_cast) that states exactly what is intended and is easy to search for.

Rationale

A C-style cast (T)expr silently performs whichever conversion compiles — including reinterpret- or const-casts — so the intent is unclear and dangerous conversions go unnoticed. Use a named cast (static_cast, const_cast, reinterpret_cast) that states exactly what is intended and is easy to search for.

The following code illustrates the pattern detected by this rule:

void f(int i, A *a) {
  // FLAGGED: C-style cast in C++ code
  double d = (double)i;

  // FLAGGED: C-style cast in C++ code
  B *b = (B *)a;

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.