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

            Bug ID: 99972
           Summary: missing -Wunused-result on a call to a locally
                    redeclared warn_unused_result function
           Product: gcc
           Version: 11.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: ---

The test case below (related to pr99420) shows that GCC doesn't consistently
propagate function attributes across redeclarations of extern functions at
function scope.

The noreturn attribute on fnr() is copied from the declaration in gnr() to the
one in hnr() and successfully suppresses -Wreturn-type in both callers.  That's
how it's supposed to work.

But the warn_unused_result on fwur() is clearly not copied from the declaration
inb gwur() to the one in hwur() because only the call to the function in gwur()
triggers -Wunused-result but the call in hwur() doesn't.  That's a bug with the
same root cause as pr99420.

$ cat z.c && gcc -S -Wall -xc++ z.c
int gnr (void)
{ 
  __attribute__ ((noreturn)) int fnr (void);
  fnr ();
  // no return, no warning (good)
}

int hnr (void)
{
  int fnr (void);

  fnr ();
  // no return, no warning (good)
}


void gwur (void)
{
  __attribute__ ((warn_unused_result)) int fwur (void);
  fwur ();   // -Wunused-result (good)
}

void hwur (void)
{
  int fwur (void); 

  fwur ();   // missing -Wunused-result (bug)
}
z.c: In function ‘int hnr()’:
z.c:14:1: warning: no return statement in function returning non-void
[-Wreturn-type]
   14 | }
      | ^
z.c: In function ‘void gwur()’:
z.c:20:8: warning: ignoring return value of ‘int fwur()’ declared with
attribute ‘warn_unused_result’ [-Wunused-result]
   20 |   fwur ();   // -Wunused-result (good)
      |   ~~~~~^~

Reply via email to