[PATCHv2] implement -Wold-style-cast

Alp Toker alp at nuanti.com
Mon Nov 25 21:37:07 PST 2013

On 22/11/2013 15:54, Ondřej Hošek wrote:
> I've been trying to implement the relevant fix-it, but apart from not
> being able to find the end of the expression being cast (to insert the
> closing paren), the pathological case
> On Fri, Nov 22, 2013 at 2:13 AM, Dmitri Gribenko <gribozavr at gmail.com> wrote:
>> (for example, static cast of a const cast)
> might pose even more of a challenge, because I have to find and print
> the "halfway" type:
> int i = 42;
> const int *cpi = &i;
> float *fi = (float *)cpi;
> // -> reinterpret_cast<float *>(const_cast<int *>(cpi));
> This already spells trouble with regard to code formatting, and it
> gets worse if I have a multi-level pointer type where some of the
> levels are const and some aren't.

Hi Ondřej,

I suspect the complexity of converting complex / multi-level C-style 
casts to C++-style casts just isn't worth the effort.

Even if you get it to work, the results wouldn't be pretty. It's better 
to encourage the developer to find an idiomatic solution by 
restructuring their code. Thanks for investigating this though.

I'd say it's fine just to provide FixIt replacements for the common 
cases like straight const_cast<> that are easy to determine using 
existing CastOperation machinery.

> (Also, I have to move the warning from Parser to Sema because only
> then do I have the type information to generate a fix-it, so I don't
> know at all how well it will work with templates.)

It's also fine to work on this incrementally if you could prepare a 
patch similar to the original one but moving the check to the right 
place in Sema, along with a TODO about the FixIt. (It shouldn't be added 
to Parse because that's a clear dead-end.)

I don't know what user expectations are for -Wold-style-cast, but it 
might be worth silently permitting the idiomatic C-style CK_ToVoid used 
to suppress unused variable warnings as a special case. Do you have an 
opinion on that?


> Cheers,
> ~~ Ondra
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

the browser experts

More information about the cfe-commits mailing list