11
votes

I'd like to use the -Wunused-result option in GCC to detect unused/ignored return values in my code. But this requires marking the function declarations either with an attribute, or with attribute specifier sequence.

Doing this by hand could be very time consuming and error-prone. I was wondering if there is a way to tell GCC to set attributes for all functions (or set a default attribute), so that I could use the warning flag.

1
Many functions are fine if their result is ignored. - SLaks
Well, I tried the option at first, and I got no warnings so I googled what to do, and this answer suggests the attributes. - Machta
yeah sorry, I also just tried it and it seems like the docs arent accurate - 463035818_is_not_a_number
why do you need that warning btw? - 463035818_is_not_a_number
in that case that warning alone wont help, because it wont catch cases where a call is auto x = foo(); but then ignoring x, though in combination with the unused variable warning you might be safe - 463035818_is_not_a_number

1 Answers

1
votes

This looks like a job for a GCC plugin. Upstream might also accept a patch which sets the attribute for all subsequent functions using a pragma, but the exact semantics are probably quite tricky for C++.