[clang-tools-extra] db335d0 - [clang-tidy] Ignore cxxRewrittenBinaryOperator in defaulted function decls in modernize-use-nullptr

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 25 06:46:50 PST 2022


Author: Jens Massberg
Date: 2022-11-25T15:45:37+01:00
New Revision: db335d02a5e7d98c24f7006e42129856ba1cd695

URL: https://github.com/llvm/llvm-project/commit/db335d02a5e7d98c24f7006e42129856ba1cd695
DIFF: https://github.com/llvm/llvm-project/commit/db335d02a5e7d98c24f7006e42129856ba1cd695.diff

LOG: [clang-tidy] Ignore cxxRewrittenBinaryOperator in defaulted function decls in modernize-use-nullptr

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.

Patch by Jens Massberg.

Reviewed By: ilya-biryukov

Differential Revision: https://reviews.llvm.org/D138701

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-cxx20.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
index 5ca2be2eb556d..220a310a0d4e4 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
@@ -62,7 +62,9 @@ StatementMatcher makeCastSequenceMatcher() {
                         ImplicitCastToNull,
                         hasAncestor(cxxRewrittenBinaryOperator().bind(
                             "checkBinopOperands")))
-                        .bind(CastSequence))))));
+                        .bind(CastSequence))),
+                // Skip defaulted comparison operators.
+                unless(hasAncestor(functionDecl(isDefaulted()))))));
 }
 
 bool isReplaceableRange(SourceLocation StartLoc, SourceLocation EndLoc,

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-cxx20.cpp
index b00cb5787ea07..be59d4ed1d056 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-cxx20.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-cxx20.cpp
@@ -1,9 +1,33 @@
 // RUN: %check_clang_tidy -std=c++20 %s modernize-use-nullptr %t
 
 namespace std {
+class strong_ordering;
+
+// Mock how STD defined unspecified parameters for the operators below.
+struct _CmpUnspecifiedParam {
+  consteval
+  _CmpUnspecifiedParam(int _CmpUnspecifiedParam::*) noexcept {}
+};
+
 struct strong_ordering {
-  int n;
-  constexpr operator int() const { return n; }
+  signed char value;
+
+  friend constexpr bool operator==(strong_ordering v,
+                                   _CmpUnspecifiedParam) noexcept {
+    return v.value == 0;
+  }
+  friend constexpr bool operator<(strong_ordering v,
+                                  _CmpUnspecifiedParam) noexcept {
+    return v.value < 0;
+  }
+  friend constexpr bool operator>(strong_ordering v,
+                                  _CmpUnspecifiedParam) noexcept {
+    return v.value > 0;
+  }
+  friend constexpr bool operator>=(strong_ordering v,
+                                   _CmpUnspecifiedParam) noexcept {
+    return v.value >= 0;
+  }
   static const strong_ordering equal, greater, less;
 };
 constexpr strong_ordering strong_ordering::equal = {0};
@@ -12,8 +36,10 @@ constexpr strong_ordering strong_ordering::less = {-1};
 } // namespace std
 
 class A {
+  int a;
 public:
   auto operator<=>(const A &other) const = default;
+  // CHECK-FIXES: auto operator<=>(const A &other) const = default;
 };
 
 void test_cxx_rewritten_binary_ops() {
@@ -32,3 +58,14 @@ void test_cxx_rewritten_binary_ops() {
   result = (a1 > ((a1 > (ptr == 0 ? a1 : a2)) ? a1 : a2));
   // CHECK-FIXES: result = (a1 > ((a1 > (ptr == nullptr ? a1 : a2)) ? a1 : a2));
 }
+
+template<class T1, class T2>
+struct P {
+  T1 x1;
+  T2 x2;
+  friend auto operator<=>(const P&, const P&) = default;
+  // CHECK-FIXES: friend auto operator<=>(const P&, const P&) = default;
+};
+
+bool foo(P<int,int> x, P<int, int> y) { return x < y; }
+// CHECK-FIXES: bool foo(P<int,int> x, P<int, int> y) { return x < y; }


        


More information about the cfe-commits mailing list