A
A
Andrey Kaprov2011-09-28 08:48:51
C++ / C#
Andrey Kaprov, 2011-09-28 08:48:51

C++, search for explicit type conversions: a = (int)b;?

A question about diagnostics.
One person suggested to implement in the PVS-Studio analyzer a search for all explicit type conversions in the C style. Those. detect constructions of the form:
int *x = (int *)y;
float a = float(b);
float c = (float)(d);
The goal is to replace all of these casts with safer options - reinterpret_cast / static_cast / const_cast. In the process of such refactoring, some defects may well be revealed.
Of course, this is not the detection of real errors. And if this diagnostic is implemented, it will be located in the [Customer's Specific Requests] section and disabled by default.
However, even in the benefits of this option, I'm not sure. Decided to ask a question. Does anyone else need to look up all the explicit C-style casts? Would anyone like to do similar refactoring of their code?

Answer the question

In order to leave comments, you need to log in

8 answer(s)
N
Next_Alex, 2011-09-28
@Next_Alex

Lint does this, and it's been useful a few times.

V
Vasily Sorokin, 2011-09-28
@Vass

Definitely necessary. C-style type casting is an evil that needs to be fought.

T
Tujh, 2011-09-28
@Tujh

Costs.

A
Andrey Kaprov, 2011-09-28
@Andrey2008

It's clear. There is interest. So it will be. Thank you.

C
CaptainTrunky, 2011-09-28
@CaptainTrunky

A very useful feature. Repeatedly rewrote both my own code and the inherited one, in order to translate everything into the C++ version.

R
Riateche, 2011-09-29
@Riateche

You wrote that you don't want to duplicate compiler checks. g++ has a -Wold-style-cast switch that turns on warnings about such type casts. From this we can conclude that support for this option should not be implemented. I think you should look for more hard-to-find bugs instead.

A
Andrey Kaprov, 2011-10-12
@Andrey2008

Implemented in PVS-Studio 4.38. www.viva64.com/ru/d/0201/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question