This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/82134] New: warn_unused_result triggers on empty structs even when they are used


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

            Bug ID: 82134
           Summary: warn_unused_result triggers on empty structs even when
                    they are used
           Product: gcc
           Version: 7.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zackw at panix dot com
  Target Milestone: ---

If a function that returns an empty struct is tagged with attribute
warn_unused_result, any call will trigger the warning, even if the return value
is (nominally) used.  For instance

struct Empty {};

extern void use_empty(struct Empty);

__attribute__((warn_unused_result))
extern struct Empty make_empty(void);

void should_warn(void)
{
    make_empty();
}

void shouldnt_warn_1(void)
{
    use_empty(make_empty());
}

void shouldnt_warn_2(void)
{
    struct Empty e = make_empty();
    use_empty(e);
}

-->

test.c: In function ‘should_warn’:
test.c:10:5: warning: ignoring return value of ‘make_empty’, declared with
attribute warn_unused_result [-Wunused-result]
     make_empty();
     ^~~~~~~~~~~~
test.c: In function ‘shouldnt_warn_1’:
test.c:15:5: warning: ignoring return value of ‘make_empty’, declared with
attribute warn_unused_result [-Wunused-result]
     use_empty(make_empty());
     ^~~~~~~~~~~~~~~~~~~~~~~
test.c: In function ‘shouldnt_warn_2’:
test.c:20:22: warning: ignoring return value of ‘make_empty’, declared with
attribute warn_unused_result [-Wunused-result]
     struct Empty e = make_empty();
                      ^~~~~~~~~~~~

with both GCC 6 and GCC 7.

(From here:
https://stackoverflow.com/questions/46096628/gcc-empty-structs-and-wunused-result)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]