[clang-tidy] Adding a new use nodiscard checker (help needed)

Hi,

That may be a really nice check indeed.
I think that a good way to proceed would be to start a review on Phabricator (https://reviews.llvm.org/).

From what I’ve seen there are some things that you could achieve through AST matchers instead of checking conditions when already having a candidate match.

For instance, instead of:

Finder->addMatcher(
cxxMethodDecl(eachOf(unless(returns(voidType())), (isConst())))
.bind(“noDiscardCandidate”),
this);

and then if (MatchedDecl->hasUnusedResultAttr())

you can just add unless(hasAttr(clang::attr::WarnUnusedResult)) inside eachOf.

And for the hint insertion location, these 3 should always be fine for you even in the case of trailing return type:

  • “getBeginLoc()”

  • “getInnerLocStart()”

  • “getOuterLocStart()”

See slightly modified Stephen’s example at: http://ec2-52-14-16-249.us-east-2.compute.amazonaws.com:10240/z/8GsU8c.

Best regards,
Marek