Forbid needless pointer casts

Mathias Lang pro.mathias.lang at gmail.com
Thu Feb 20 11:41:18 UTC 2020


On Thursday, 20 February 2020 at 11:31:17 UTC, FlameInHeart wrote:
> Hey,
>
> How about forbidding this:
>
> void foo(Monkey* m) {
>     Monkey* m2 = cast(Monkey*) m;
> }
>
> Pointer casts are potentially nasty when refactoring and the 
> needless ones are easy for the compiler to detect, and probably 
> no-one will find objections to this change.
>
> Just an easy win, isn't it?
>
> (not going to write a DIP for this...)

Why would we forbid something that is legal ? D is usually quite 
liberal, allowing you to do things as long as they're not 
obviously wrong. The good thing is that you don't end up 
"pleasing the compiler" as you do in C++, where you have tons of 
false positives...

And in this case, it is obviously pointless, but consider generic 
code:
```
void foo (T) (T* m) {
     T* m2 = cast(Unqual!(T)*) m;
}
```


More information about the Digitalmars-d mailing list