This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Improve cppcoreguidelines-avoid-reference-coroutine-parameters check
ClosedPublic

Authored by PiotrZSL on Aug 23 2023, 1:12 PM.

Details

Summary

Ignore false positives related to matching parameters of non
coroutine functions and increase issue detection for cases
involving type aliases with references.

Fixes: #64915

Diff Detail

Event Timeline

PiotrZSL created this revision.Aug 23 2023, 1:12 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 23 2023, 1:12 PM
PiotrZSL requested review of this revision.Aug 23 2023, 1:12 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 23 2023, 1:12 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
ccotter accepted this revision.Aug 23 2023, 8:14 PM

Thanks!

clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidReferenceCoroutineParametersCheck.cpp
20

Can we do this by matching if any parameter is of reference type, rather than relying on the logic living in check (I wrote this originally, although perhaps this would be an improvement). Happy to take this up as a followup...

This revision is now accepted and ready to land.Aug 23 2023, 8:14 PM
PiotrZSL added inline comments.Aug 23 2023, 9:44 PM
clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidReferenceCoroutineParametersCheck.cpp
20

Matching functionDecl first is cheaper...
In theory using hasParent instead of hasAncestor should be sufficient, but I never trusted that relation.

Something like this should do a trick also:

functionDecl(unless(parameterCountIs(0)), hasBody(coroutineBodyStmt()), forEach(parmVarDecl(hasType(qualType(hasCanonicalType(	references(qualType()))))).bind("param"))

should also work, but I didn't test it.
Current code will do a same thing, not using matchers should even make it faster, and check method is called only for coroutines.

Personally I just prefer matcher to match nodes that we got less.
This also enable other thing, like providing 1 warning per coroutine and additional notes for every parameter with parameter name in it.

PiotrZSL marked an inline comment as done.Aug 24 2023, 2:49 PM

I'm pushing this in current state, if you want to change something after that, fell free to do it.