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

            Bug ID: 103363
           Summary: confusing -Wnonnull-compare testing a reference
                    argument for equality to null
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

Since the conversion of the address of a reference to bool is diagnosed by
-Waddress, issuing a second warning for it isn't necessary.  Mentioning
attribute nonnull where there is one in the source code seems confusing.

$ cat t.C && gcc -O2 -S -Wall t.C
struct S { int i; };

bool f (const S &s)
{
  return &s;
}
t.C: In function ‘bool f(const S&)’:
t.C:5:10: warning: the compiler can assume that the address of ‘s’ will never
be NULL [-Waddress]
    5 |   return &s;
      |          ^~
t.C:3:18: note: ‘s’ declared here
    3 | bool f (const S &s)
      |         ~~~~~~~~~^
t.C:5:11: warning: ‘nonnull’ argument ‘s’ compared to NULL [-Wnonnull-compare]
    5 |   return &s;
      |           ^


Clang issues just one warning (though the wording doesn't seem ideal either:
there's no evidence of dereferencing a null pointer so assuming that's what
necessarily led the programmer to the test doesn't seem warranted):
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 &s;

Reply via email to