The option -Wsuggest-attribute= helps improve code generation by annotating function declarations with attributes like cold, const, pure, malloc and noreturn. However, the option doesn't make it possible to request suggestions for the returns_nonnull attribute. Especially in C++ where functions returning newly allocated memory often never return null (because they throw on failure to allocate), making use of the attribute could further improve generated code. $ cat t.C && gcc -Wall -Wextra -O -S -Wsuggest-attribute=noreturn t.C int f () // -Wsuggest-attribute=noreturn here (good) { throw "not implemented"; } void* f (unsigned n) // missing -Wsuggest-attribute=returns_nonnull { return new char[n]; } t.C: In function ‘int f()’: t.C:1:5: warning: function might be candidate for attribute ‘noreturn’ [-Wsuggest-attribute=noreturn] int f () // -Wsuggest-attribute=noreturn here (good) ^
See also bug 84202.
I just noticed -Wsuggest-attribute=returns_nonnull mentioned in bug 58689 comment #8. I'm on the fence between resolving this as a duplicate of that bug and treating pr58689 as a meta-bug. Let me go with the latter for now and make this a blocker of it.
(In reply to Martin Sebor from comment #2) > I just noticed -Wsuggest-attribute=returns_nonnull mentioned in bug 58689 > comment #8. I'm on the fence between resolving this as a duplicate of that > bug and treating pr58689 as a meta-bug. Let me go with the latter for now > and make this a blocker of it. ok confirming this one too then
This is implemented in gcc 14: commit 53ba8d66955 Author: Jan Hubicka <jh@suse.cz> Date: Mon Nov 20 19:35:53 2023 inter-procedural value range propagation implement very basic propapgation of return value ranges from VRP pass. This helps std::vector's push_back since we work out value range of allocated block. This propagates only within single translation unit. I hoped we will also do the propagation at WPA stage, but that needs more work on ipa-cp side. I also added code auto-detecting return_nonnull and corresponding -Wsuggest-attribute. [It is misspelled in the commit message.]
.