https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86879
Bug ID: 86879
Summary: G++ should warn about redundant tests for null
pointers returned from functions with
__attribute__((returns_nonnull))
Product: gcc
Version: 9.0
Status: UNCONFIRMED
Keywords: diagnostic
Severity: enhancement
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: redi at gcc dot gnu.org
Target Milestone: ---
void* get() __attribute__((returns_nonnull));
int main() { return get() ? 0 : 1; }
This compiles without any diagnostic, but the check for a null pointer is
redundant because of the attribute.
It seems like this would fit under -Wnonnull-compare
Also, this should produce a similar warning:
int& ref();
int main()
{
return &ref() ? 0 : 1;
}
This check can never fail, because a reference cannot be bound to a null
pointer, but G++ is silent. Clang says:
nonnull.cc:5:11: warning: reference cannot be bound to dereferenced null
pointer in well-defined C++ code; pointer may be assumed to always convert to
true [-Wundefined-bool-conversion]
return &ref() ? 0 : 1;
^~~~~ ~
nonnull.cc:1:6: note: 'ref' returns a reference
int& ref();
^
1 warning generated.