[PATCH] Warning on unused results in an unevaluated context

Richard Smith richard at metafoo.co.uk
Thu Oct 16 13:05:02 PDT 2014


It would be better to have the warning and no-warning cases for typeid next
to each other. Otherwise, LGTM, thanks!

Does this also fix the same issue for -Wunused-comparison?

On Thu, Oct 16, 2014 at 11:58 AM, Aaron Ballman <aaron at aaronballman.com>
wrote:

> Patch attached with more test cases; the typeid case you were
> concerned is also addressed in this patch.
>
> ~Aaron
>
> On Thu, Oct 16, 2014 at 2:27 PM, Aaron Ballman <aaron at aaronballman.com>
> wrote:
> > 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20141016/f046b6ca/attachment.html>


More information about the cfe-commits mailing list