D-Scanner
D-Scanner copied to clipboard
opAssign return type check
Returning void from opAssign should be forbidden, it should most of the time return the type itself so that code like this doesn't happen: https://github.com/dlang/phobos/commit/59520969ef73eaf0691972ee00b389e5bbc4c8fb#diff-4715499b2ff2d74e4eb3c6f3909c611cR673
else if (cast(bool)(match = diagnostic.message.matchFirst(undefinedIdentifier))
|| cast(bool)(match = diagnostic.message.matchFirst(undefinedTemplate))
|| cast(bool)(match = diagnostic.message.matchFirst(noProperty)))
->
source/served/extension.d(1087,30): Error: cannot cast expression match.opAssign(matchFirst(diagnostic.message, undefinedIdentifier)) of type void to bool
source/served/extension.d(1088,26): Error: cannot cast expression match.opAssign(matchFirst(diagnostic.message, undefinedTemplate)) of type void to bool
source/served/extension.d(1089,26): Error: cannot cast expression match.opAssign(matchFirst(diagnostic.message, noProperty)) of type void to bool
Ha. I actually wish the language had gone the other way and made = always return void for everything.
if it would just automatically infer it that you want the instance of it in opAssign and properties returning void it would be far nicer, but sadly it's not doing it like that
I'm no sure if it's supposed to work like that. Consider this
bool b;
if (b = false){} // Error: assignment cannot be used as a condition, perhaps `==` was meant?
if (cast(bool()(b = false)){} // Error: assignment cannot be used as a condition, perhaps `==` was meant?
It means that you want to enforce something that doesn't even work for the basic types of the language.
So i'd go for a close as wontfix. What do you think ?
int fn() { return 5; }
int i;
if (cast(bool)(i = fn())) {} // works