This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Ignore cxxRewrittenBinaryOperator in defaulted function decls in modernize-use-nullptr.
ClosedPublic

Authored by massberg on Nov 25 2022, 2:25 AM.

Details

Summary

The check has produced false positives when checking the default implementation of the spaceship operator.
The default implementation should be skipped by the check.

Modified the existing test so that the check runs into the bug without this fix and add another test case.

Fixes #53961

Diff Detail

Event Timeline

massberg created this revision.Nov 25 2022, 2:25 AM
Herald added a project: Restricted Project. · View Herald Transcript
massberg requested review of this revision.Nov 25 2022, 2:25 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 25 2022, 2:25 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
ilya-biryukov accepted this revision.Nov 25 2022, 4:47 AM

LGTM, thanks!
Initially I thought that traversing with TK_IgnoreUnlessSpelledInSource might be appropriate. But that idea does not seem to work as we do need to match implicit casts to find the nullptr.

I am also not sure if we should show this warning when the generated code was written by hand. The check will keep suggesting to replace (a <=> b) < 0 with (a <=> b) < nullptr.
Technically this will compile, but the C++ Standard explicitly mentions the comparisons must be implicitly rewritten to (a <=> b) < 0, not nullptr.
But let's not solve this problem until someone actually reports it.

clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
66

NIT: maybe add a comment mentioning that this tries to find defaulted comparison operators.
Pre-C++20 one could only put = default constructors, destructors and assignment operators. So I suspect this might be a useful clarification for readers of the code.

clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-cxx20.cpp
6

NIT: add a comment mentioning that this mocks how STL defined the parameter.

This revision is now accepted and ready to land.Nov 25 2022, 4:47 AM
massberg updated this revision to Diff 477941.Nov 25 2022, 6:19 AM

Add comments.

massberg marked 2 inline comments as done.Nov 25 2022, 6:20 AM

Thanks for the comments!

This revision was landed with ongoing or failed builds.Nov 25 2022, 6:46 AM
This revision was automatically updated to reflect the committed changes.