This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix `readability-container-size-empty` check for smart pointers
ClosedPublic

Authored by fwolff on Dec 5 2021, 12:40 PM.

Details

Summary

Fixes PR#51776. If the object on which size() is called has an overloaded operator->, the span for E will include the ->, and since the return type of operator-> is a pointer, readability-container-size-empty will try to add another arrow, leading to the nonsensical vp->->empty() suggestion. This patch fixes this behavior.

Diff Detail

Event Timeline

fwolff created this revision.Dec 5 2021, 12:40 PM
fwolff requested review of this revision.Dec 5 2021, 12:41 PM
aaron.ballman accepted this revision.Dec 8 2021, 7:12 AM

LGTM! Can you also add a release note for the fix?

This revision is now accepted and ready to land.Dec 8 2021, 7:12 AM
Sockke requested changes to this revision.Dec 24 2021, 2:05 AM

Could you please add a test case where the smart pointer object is dereferenced before calling size()? E.g. return (*ptr).size() == 0;. I think this change doesn't work on this test case.

This revision now requires changes to proceed.Dec 24 2021, 2:05 AM
fwolff updated this revision to Diff 400625.Jan 17 2022, 1:03 PM

Could you please add a test case where the smart pointer object is dereferenced before calling size()? E.g. return (*ptr).size() == 0;. I think this change doesn't work on this test case.

This is not entirely true; what you're seeing is an existing, unrelated bug: https://godbolt.org/z/9zEfdrPW8

In any case, I've fixed it, added a test for it, and added a bullet point to the release notes.

Sockke accepted this revision.EditedJan 17 2022, 11:32 PM

Thanks for improving this check! I think it looks great and safe enough. It would be better to turn !(*ptr).empty() into !ptr->empty(), but I have no particular opinions at this point. Let's see if @aaron.ballman has any comments.

This revision is now accepted and ready to land.Jan 17 2022, 11:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 20 2022, 8:06 AM