[PATCH] D95726: clang-tidy: modernize-use-nullptr mistakenly fixes rewritten spaceship operator comparisons - hasGrandparent version

Conrad Poelman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 29 21:12:35 PST 2021


poelmanc created this revision.
poelmanc added reviewers: aaron.ballman, angelgarcia, hokein.
poelmanc added a project: clang-tools-extra.
Herald added a subscriber: yaxunl.
poelmanc requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

As in https://reviews.llvm.org/D95714, `clang-tidy -std=c++20` with `modernize-use-nullptr` mistakenly inserts `nullptr` in place of the comparison operator if the comparison internally expands in the AST to a rewritten spaceship operator. This can be reproduced by running the new `modernize-use-nullptr-cxx20.cpp` test without applying the supplied patch to UseNullptrCheck.cpp; the current clang-tidy will mistakenly replace:

  result = (a1 < a2);

with

  result = (a1 nullptr a2);

Oops!

The supplied patch fixes this by adding just one comment and one line of code to UseNullptrCheck.cpp:

  // Skip implicit casts to 0 generated by operator<=> rewrites.
  unless(hasGrandparent(cxxRewrittenBinaryOperator())),

It also adds the new `hasGrandparent` matcher, which is necessary as opposed to `hasAncestor` to properly convert:

  result = (a1 > (ptr == 0 ? a1 : a2));

to

  result = (a1 > (ptr == nullptr ? a1 : a2));

In this case the AST has an "ancestor" that is a rewritten binary operator, but not a direct grandparent.

This is an alternative to https://reviews.llvm.org/D95714.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95726

Files:
  clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize-use-nullptr-cxx20.cpp
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/include/clang/ASTMatchers/ASTMatchersInternal.h
  clang/lib/ASTMatchers/ASTMatchFinder.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95726.320275.patch
Type: text/x-patch
Size: 5271 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210130/de7e5568/attachment-0001.bin>


More information about the cfe-commits mailing list