[clang-tools-extra] [clang-tidy] Fix invalid fixit from modernize-use-ranges for nullptr used with std::unique_ptr (PR #127162)

via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 14 06:11:30 PST 2025


https://github.com/Andrewyuan34 updated https://github.com/llvm/llvm-project/pull/127162

>From 78d04102fc6d6ead4ab1153a82ed2ab9d8437bed Mon Sep 17 00:00:00 2001
From: Andrewyuan34 <yiboy at uvic.ca>
Date: Thu, 13 Feb 2025 22:35:36 -0500
Subject: [PATCH 1/5] [clang-tidy] Fix invalid fixit from modernize-use-ranges
 for nullptr used with std::unique_ptr

---
 .../clang-tidy/utils/UseRangesCheck.cpp       | 10 ++++++++
 .../checkers/modernize/use-ranges.cpp         | 25 ++++++++++++++++---
 2 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
index aba4d17ccd035..4c5db488dce7f 100644
--- a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
@@ -215,6 +215,16 @@ void UseRangesCheck::check(const MatchFinder::MatchResult &Result) {
     const auto *Call = Result.Nodes.getNodeAs<CallExpr>(Buffer);
     if (!Call)
       continue;
+    if (Function->getName() == "find") {
+      unsigned ValueArgIndex = 2;
+      if (Call->getNumArgs() <= ValueArgIndex)
+        continue;
+      const Expr *ValueExpr =
+          Call->getArg(ValueArgIndex)->IgnoreParenImpCasts();
+      if (isa<CXXNullPtrLiteralExpr>(ValueExpr)) {
+        return;
+      }
+    }
     auto Diag = createDiag(*Call);
     if (auto ReplaceName = Replacer->getReplaceName(*Function))
       Diag << FixItHint::CreateReplacement(Call->getCallee()->getSourceRange(),
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
index b022efebfdf4d..57ca038f64511 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
@@ -1,14 +1,24 @@
-// RUN: %check_clang_tidy -std=c++20 %s modernize-use-ranges %t -- -- -I %S/Inputs/use-ranges/
-// RUN: %check_clang_tidy -std=c++23 %s modernize-use-ranges %t -check-suffixes=,CPP23 -- -I %S/Inputs/use-ranges/
+// RUN: %check_clang_tidy -std=c++20 %s modernize-use-ranges %t -- -- -I %S/Inputs/
+// RUN: %check_clang_tidy -std=c++23 %s modernize-use-ranges %t -check-suffixes=,CPP23 -- -I %S/Inputs/
+// Example: ./check_clang_tidy.py -std=c++20 checkers/modernize/use-ranges.cpp modernize-use-ranges temp.txt -- -- -I ~/llvm-project/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/
 
 // CHECK-FIXES: #include <algorithm>
 // CHECK-FIXES-CPP23: #include <numeric>
 // CHECK-FIXES: #include <ranges>
 
-#include "fake_std.h"
+#include "use-ranges/fake_std.h"
+#include "smart-ptr/unique_ptr.h"
 
 void Positives() {
   std::vector<int> I, J;
+
+  // Expect to have no check messages
+  std::find(I.begin(), I.end(), nullptr);
+
+  std::find(I.begin(), I.end(), std::unique_ptr<int>());
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
+  // CHECK-FIXES: std::ranges::find(I, std::unique_ptr<int>());
+
   std::find(I.begin(), I.end(), 0);
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
   // CHECK-FIXES: std::ranges::find(I, 0);
@@ -76,6 +86,14 @@ void Positives() {
 
 void Reverse(){
   std::vector<int> I, J;
+  
+  // Expect to have no check messages
+  std::find(I.rbegin(), I.rend(), nullptr);
+
+  std::find(I.rbegin(), I.rend(), std::unique_ptr<int>());
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
+  // CHECK-FIXES: std::ranges::find(std::ranges::reverse_view(I), std::unique_ptr<int>());
+
   std::find(I.rbegin(), I.rend(), 0);
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
   // CHECK-FIXES: std::ranges::find(std::ranges::reverse_view(I), 0);
@@ -112,3 +130,4 @@ void Negatives() {
   // Pathological, but probably shouldn't diagnose this
   std::rotate(I.begin(), I.end(), I.end() + 0);
 }
+

>From 6d83f551843603cd76dafb0de48577385aad58a6 Mon Sep 17 00:00:00 2001
From: Andrewyuan34 <yiboy at uvic.ca>
Date: Fri, 14 Feb 2025 01:15:46 -0500
Subject: [PATCH 2/5] Based on PR reviews, improve code quality by making sure
 const correctness and modifying code format

Co-authored-by: Piotr Zegar <me at piotrzegar.pl>
---
 .../clang-tidy/utils/UseRangesCheck.cpp           |  5 ++---
 clang-tools-extra/docs/ReleaseNotes.rst           |  3 +++
 .../clang-tidy/checkers/modernize/use-ranges.cpp  | 15 ++++++++-------
 3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
index 4c5db488dce7f..ce86a27ad69b2 100644
--- a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
@@ -216,14 +216,13 @@ void UseRangesCheck::check(const MatchFinder::MatchResult &Result) {
     if (!Call)
       continue;
     if (Function->getName() == "find") {
-      unsigned ValueArgIndex = 2;
+      const unsigned ValueArgIndex = 2;
       if (Call->getNumArgs() <= ValueArgIndex)
         continue;
       const Expr *ValueExpr =
           Call->getArg(ValueArgIndex)->IgnoreParenImpCasts();
-      if (isa<CXXNullPtrLiteralExpr>(ValueExpr)) {
+      if (isa<CXXNullPtrLiteralExpr>(ValueExpr))
         return;
-      }
     }
     auto Diag = createDiag(*Call);
     if (auto ReplaceName = Replacer->getReplaceName(*Function))
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 6b8fe22242417..5ad4fb7270ac1 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -109,6 +109,9 @@ Changes in existing checks
 - Improved :doc:`misc-redundant-expression
   <clang-tidy/checks/misc/redundant-expression>` check by providing additional
   examples and fixing some macro related false positives.
+  
+- Fixed a false positive in :doc:`modernize-use-ranges
+  <clang-tidy/checks/modernize/use-ranges>`updat the logic to suppress warnings for `nullptr` in `std::find`.
 
 Removed checks
 ^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
index 57ca038f64511..5aa026038b1cd 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
@@ -11,13 +11,14 @@
 
 void Positives() {
   std::vector<int> I, J;
+  std::vector<std::unique_ptr<int>> K;
 
   // Expect to have no check messages
-  std::find(I.begin(), I.end(), nullptr);
+  std::find(K.begin(), K.end(), nullptr);
 
-  std::find(I.begin(), I.end(), std::unique_ptr<int>());
+  std::find(K.begin(), K.end(), std::unique_ptr<int>());
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
-  // CHECK-FIXES: std::ranges::find(I, std::unique_ptr<int>());
+  // CHECK-FIXES: std::ranges::find(K, std::unique_ptr<int>());
 
   std::find(I.begin(), I.end(), 0);
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
@@ -86,13 +87,14 @@ void Positives() {
 
 void Reverse(){
   std::vector<int> I, J;
+  std::vector<std::unique_ptr<int>> K;
   
   // Expect to have no check messages
-  std::find(I.rbegin(), I.rend(), nullptr);
+  std::find(K.rbegin(), K.rend(), nullptr);
 
-  std::find(I.rbegin(), I.rend(), std::unique_ptr<int>());
+  std::find(K.rbegin(), K.rend(), std::unique_ptr<int>());
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
-  // CHECK-FIXES: std::ranges::find(std::ranges::reverse_view(I), std::unique_ptr<int>());
+  // CHECK-FIXES: std::ranges::find(std::ranges::reverse_view(K), std::unique_ptr<int>());
 
   std::find(I.rbegin(), I.rend(), 0);
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
@@ -130,4 +132,3 @@ void Negatives() {
   // Pathological, but probably shouldn't diagnose this
   std::rotate(I.begin(), I.end(), I.end() + 0);
 }
-

>From 50483b8cb4e0ad6c8fd3ca89b8e98d4a47b20283 Mon Sep 17 00:00:00 2001
From: Andrewyuan34 <yiboy at uvic.ca>
Date: Fri, 14 Feb 2025 02:26:34 -0500
Subject: [PATCH 3/5] Update ReleaseNotes.rst

---
 clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 5ad4fb7270ac1..371415c6894b0 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -111,7 +111,7 @@ Changes in existing checks
   examples and fixing some macro related false positives.
   
 - Fixed a false positive in :doc:`modernize-use-ranges
-  <clang-tidy/checks/modernize/use-ranges>`updat the logic to suppress warnings for `nullptr` in `std::find`.
+  <clang-tidy/checks/modernize/use-ranges>`update the logic to suppress warnings for `nullptr` in `std::find`.
 
 Removed checks
 ^^^^^^^^^^^^^^

>From 8987e809f95bb946705e29da47e946fb8d06a51e Mon Sep 17 00:00:00 2001
From: Andrewyuan34 <yiboy at uvic.ca>
Date: Fri, 14 Feb 2025 09:11:01 -0500
Subject: [PATCH 4/5] Update clang-tools-extra/docs/ReleaseNotes.rst

Co-authored-by: Baranov Victor <70346889+vbvictor at users.noreply.github.com>
---
 clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 371415c6894b0..31478348b9378 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -110,7 +110,7 @@ Changes in existing checks
   <clang-tidy/checks/misc/redundant-expression>` check by providing additional
   examples and fixing some macro related false positives.
   
-- Fixed a false positive in :doc:`modernize-use-ranges
+- Improved :doc:`modernize-use-ranges
   <clang-tidy/checks/modernize/use-ranges>`update the logic to suppress warnings for `nullptr` in `std::find`.
 
 Removed checks

>From dc76c628f32dcacd20a3b6f97dea7b35bafbc531 Mon Sep 17 00:00:00 2001
From: Andrewyuan34 <yiboy at uvic.ca>
Date: Fri, 14 Feb 2025 09:11:20 -0500
Subject: [PATCH 5/5] Update clang-tools-extra/docs/ReleaseNotes.rst

Co-authored-by: Baranov Victor <70346889+vbvictor at users.noreply.github.com>
---
 clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 31478348b9378..2e6bd1a02aa9d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -111,7 +111,7 @@ Changes in existing checks
   examples and fixing some macro related false positives.
   
 - Improved :doc:`modernize-use-ranges
-  <clang-tidy/checks/modernize/use-ranges>`update the logic to suppress warnings for `nullptr` in `std::find`.
+  <clang-tidy/checks/modernize/use-ranges>` check by updating suppress warnings logic for ``nullptr`` in ``std::find``.
 
 Removed checks
 ^^^^^^^^^^^^^^



More information about the cfe-commits mailing list