[PATCH] Warning on unused results in an unevaluated context

Aaron Ballman aaron at aaronballman.com
Thu Oct 16 11:27:20 PDT 2014


On Thu, Oct 16, 2014 at 11:28 AM, Aaron Ballman <aaron at aaronballman.com> wrote:
> On Thu, Oct 16, 2014 at 10:23 AM, Aaron Ballman <aaron at aaronballman.com> wrote:
>> On Thu, Oct 16, 2014 at 9:05 AM, Aaron Ballman <aaron at aaronballman.com> wrote:
>>> On Wed, Oct 15, 2014 at 10:33 PM, Richard Smith <richard at metafoo.co.uk> wrote:
>>>> Does this still warn on cases like:
>>>>
>>>>   typeid(f(), g());
>>>>
>>>> where we don't know we've got an evaluated operand until after we've
>>>> finished parsing it?
>>>>
>>>> ... Hmm, we don't seem to get any warning on that case right now. =( Might
>>>> be worth someone looking into that, maybe there are other cases we're
>>>> missing.
>>
>> I'd like to make sure my understanding is correct. Given:
>>
>> #include <typeinfo>
>>
>> struct B {
>>   virtual int f();
>> };
>>
>> struct D : B {
>>   virtual int f();
>> };
>>
>> B *h() __attribute__((warn_unused_result));
>> B *h() { return new D; }
>>
>> int f() __attribute__((warn_unused_result));
>> int g();
>>
>> int main() {
>>   const auto &t1 = typeid(f(), g()); // Should not warn; unevaluated context?
>>   const auto &t2 = typeid(h(), g()); // Should warn; evaluated context?
>> }
>>
>> Is that the test case we're looking at for the typeid portion of this?
>
> I don't think it is. The result of h(), g() is still int, which is not
> a glvalue of polymorphic type. I'm struggling to think of a way in
> which I should trigger the unused result warning in the potentially
> evaluated case that wouldn't count as a use.

Okay, I just needed to eat lunch to figure this out. ;-)

#include <typeinfo>

struct B {
  virtual void f();
};

struct D : B {
  void f() override;
};

struct C {};

int f() __attribute__((warn_unused_result));

int main() {
  D d;
  C c;
  (void)typeid(f(), d); // Warn about unused result because d is a
glvalue of polymorphic type
  (void)typeid(f(), c); // Do not warn about unused result, because c
is not a polymorphic type
}

~Aaron



More information about the cfe-commits mailing list