www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Forbid needless pointer casts

reply FlameInHeart <validemail not.really> writes:
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...)
Feb 20 2020
next sibling parent Andre Pany <andre s-e-a-p.de> writes:
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...)
There is also the possibility to add this check to DScanner instead of the compiler. By the way, DScanner is now also easily accessible from Dub. You can just say `dub lint` and it will check your current dub package. Kind regards André
Feb 20 2020
prev sibling next sibling parent reply FlameInHeart <validemail not.really> writes:
...mm, or is there the possibility of a problem with templated or 
otherwise generated code?
Feb 20 2020
parent tsbockman <thomas.bockman gmail.com> writes:
On Thursday, 20 February 2020 at 11:38:22 UTC, FlameInHeart wrote:
 ...mm, or is there the possibility of a problem with templated 
 or otherwise generated code?
Yes, this change would needlessly complicate some template code. Having to write a bunch of extra logic just to prevent a template from emitting harmless no-op code in some circumstances is pretty painful in practice.
Feb 22 2020
prev sibling next sibling parent reply Mathias Lang <pro.mathias.lang gmail.com> writes:
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; } ```
Feb 20 2020
parent FlameInHeart <validemail not.really> writes:
On Thursday, 20 February 2020 at 11:41:18 UTC, Mathias Lang wrote:
 On Thursday, 20 February 2020 at 11:31:17 UTC, FlameInHeart 
 wrote:
...
 Pointer casts are potentially nasty when refactoring and the
 Why would we forbid something that is legal ?
"Pointer casts are potentially nasty when refactoring"
 And in this case, it is obviously pointless, but consider 
 generic code:
 ```
 void foo (T) (T* m) {
     T* m2 = cast(Unqual!(T)*) m;
 }
 ```
Yeah...
Feb 20 2020
prev sibling parent reply kindouchoud <jeyak10528 ailiking.com> writes:
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...)
Truly, this change would unnecessarily muddle some format code. Composing a lot of additional rationale just to keep a format from emanating innocuous no-operation code in certain conditions is really excruciating by and by.
Dec 25 2020
parent kindouchoud <jeyak10528 ailiking.com> writes:
On Friday, 25 December 2020 at 15:16:04 UTC, kindouchoud wrote:
 On Thursday, 20 February 2020 at 11:31:17 UTC, FlameInHeart 
 wrote:
 Hey,

 How about forbidding this:https://1921681001.id/ , 
 https://19216811.cam/ , https://jpgtopdf.onl/

 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...)
Truly, this change would unnecessarily muddle some format code. Composing a lot of additional rationale just to keep a format from emanating innocuous no-operation code in certain conditions is really excruciating by and by.
okey man
Dec 27 2020