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

Reply via email to