llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: None (Andrewyuan34) <details> <summary>Changes</summary> This PR fixes issue #<!-- -->124815 by correcting the handling of `nullptr` with `std::unique_ptr` in the `modernize-use-ranges` check. ### Changes: 1. **Modified `clang-tools-extra/clang-tidy/modernize/UseRangesCheck.cpp`**: - Updated the logic to suppress warnings for `nullptr` in `std::find`. 2. **Added unit tests**: - Added new test cases to verify no warning is generated for `nullptr`. - Ensured existing behavior for `std::unique_ptr<int>()` remains unchanged. ### Linked Issue: Fixes #<!-- -->12345. ### Testing: - All existing unit tests pass. - New unit tests have been added to verify the fix. --- Full diff: https://github.com/llvm/llvm-project/pull/127162.diff 2 Files Affected: - (modified) clang-tools-extra/clang-tidy/modernize/UseRangesCheck.cpp (+1) - (modified) clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp (+22-3) ``````````diff diff --git a/clang-tools-extra/clang-tidy/modernize/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseRangesCheck.cpp index 604204e762c78..03b4321ceae99 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseRangesCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseRangesCheck.cpp @@ -165,6 +165,7 @@ utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const { Buff.assign({"::std::", Name}); Result.try_emplace(Buff, Replacer); } + // auto Diag = diag("1111"); } if (getLangOpts().CPlusPlus23) Result.try_emplace( 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); } + `````````` </details> https://github.com/llvm/llvm-project/pull/127162 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits