This is an archive of the discontinued LLVM Phabricator instance.

Correctly handle function pointers returning a type marked nodiscard
ClosedPublic

Authored by aaron.ballman on Dec 20 2018, 12:39 PM.

Details

Summary

When a function returns a type and that type was declared [[nodiscard]], we diagnose any unused results from that call as though the function were marked nodiscard. The same behavior should apply to calls through a function pointer.

This addresses PR31526.

Diff Detail

Event Timeline

aaron.ballman created this revision.Dec 20 2018, 12:39 PM
erik.pilkington accepted this revision.Dec 20 2018, 2:12 PM

LGTM!

lib/AST/Expr.cpp
2281–2286

This duplicates some logic from FunctionDecl::hasUnusedResultAttr(), maybe we should move that member function to the CallExpr?

test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
51

This diagnostic should also probably be improved at some point, the function wasn't declared 'nodiscard', the type was.

This revision is now accepted and ready to land.Dec 20 2018, 2:12 PM
aaron.ballman marked 3 inline comments as done.

Updated based on review feedback.

Moved FunctionDecl::hasUnusedResultAttr() and FunctionDecl::getUnusedResultAttr() to CallExpr to reduce logic duplication on checking.

aaron.ballman requested review of this revision.Dec 21 2018, 12:55 PM

There's enough churn based on the review feedback that this should probably have a second round of review just to be sure.

lib/AST/Expr.cpp
2281–2286

Good suggestion -- I think that cleans up the interface nicely.

test/CXX/dcl.dcl/dcl.attr/dcl.attr.nodiscard/p2.cpp
51

Yeah, I noticed that as well.

erik.pilkington accepted this revision.Jan 2 2019, 12:27 PM

Still LG, sorry for the delay!

This revision is now accepted and ready to land.Jan 2 2019, 12:27 PM
aaron.ballman closed this revision.Jan 3 2019, 6:29 AM

Committed in r350317.