https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53215

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The result of the builtin operator new isn't unused in a new expression.
operator new() is called by the compiler to allocate memory, then it constructs
objects in that memory. So the actual allocation result isn't unused. The
pointer returned by the new expression is unused, but that's not always a bug.
This code is fine and doesn't leak:

struct X {
  X();
};

X* global;

X::X() { global = this; }

int main() {
  new X;
  delete global;
}

We could probably teach the compiler to warn about unused results of anything
with attribute__((malloc)), which would cover raw calls to the operator new()
function (but nobody calls that by mistake and forgets to use the result, so I
doubt it will catch any bugs).

More useful might be to warn about any new expression where no non-trivial
initialization happens. That would not warn about the example above (because X
has a non-trivial default constructor) but would warn about the example in
comment 0, and cases for classes with trivial default constructors.

I'm still not convinced this is a real problem that needs to be solved, but if
adding a warning isn't too hard maybe it's worth doing.

Reply via email to