Discarded return values

Simen Kjærås simen.kjaras at gmail.com
Tue Jan 30 10:49:54 UTC 2018


Is there a way to get a compile error when returning a temporary 
from a function and then not assigning it to a variable or 
passing it to a different function? E.g:

struct S {
     int[] a;
     void morph() {}
}

@warnOnDiscard
S foo() {
     return S([1,2,3]);
}

unittest {
     auto a = foo(); // Fine, assigned to variable.
     bar(foo());     // Fine, passed to other function.
     foo();          // Error: return value is discarded.
     foo().morph();  // Error: return value is discarded.
}

I know that pure functions give errors when their results are 
discarded, but in this case foo() might not be pure.
--
   Simen


More information about the Digitalmars-d-learn mailing list