Skip to content

Commit 2a3e782

Browse files
authored
[clang-tidy] Fix invalid fixit from modernize-use-ranges for nullptr used with std::unique_ptr (llvm#127162)
This PR fixes issue llvm#124815 by correcting the handling of `nullptr` with `std::unique_ptr` in the `modernize-use-ranges` check. Updated the logic to suppress warnings for `nullptr` in `std::find`.
1 parent 22a45c4 commit 2a3e782

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,19 @@ void UseRangesCheck::check(const MatchFinder::MatchResult &Result) {
215215
const auto *Call = Result.Nodes.getNodeAs<CallExpr>(Buffer);
216216
if (!Call)
217217
continue;
218+
219+
// FIXME: This check specifically handles `CXXNullPtrLiteralExpr`, but
220+
// a more general solution might be needed.
221+
if (Function->getName() == "find") {
222+
const unsigned ValueArgIndex = 2;
223+
if (Call->getNumArgs() <= ValueArgIndex)
224+
continue;
225+
const Expr *ValueExpr =
226+
Call->getArg(ValueArgIndex)->IgnoreParenImpCasts();
227+
if (isa<CXXNullPtrLiteralExpr>(ValueExpr))
228+
return;
229+
}
230+
218231
auto Diag = createDiag(*Call);
219232
if (auto ReplaceName = Replacer->getReplaceName(*Function))
220233
Diag << FixItHint::CreateReplacement(Call->getCallee()->getSourceRange(),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ Changes in existing checks
131131
- Improved :doc:`misc-redundant-expression
132132
<clang-tidy/checks/misc/redundant-expression>` check by providing additional
133133
examples and fixing some macro related false positives.
134+
135+
- Improved :doc:`modernize-use-ranges
136+
<clang-tidy/checks/modernize/use-ranges>` check by updating suppress
137+
warnings logic for ``nullptr`` in ``std::find``.
134138

135139
- Improved :doc:`misc-use-internal-linkage
136140
<clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives

clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
// RUN: %check_clang_tidy -std=c++20 %s modernize-use-ranges %t -- -- -I %S/Inputs/use-ranges/
2-
// RUN: %check_clang_tidy -std=c++23 %s modernize-use-ranges %t -check-suffixes=,CPP23 -- -I %S/Inputs/use-ranges/
1+
// RUN: %check_clang_tidy -std=c++20 %s modernize-use-ranges %t -- -- -I %S/Inputs/
2+
// RUN: %check_clang_tidy -std=c++23 %s modernize-use-ranges %t -check-suffixes=,CPP23 -- -I %S/Inputs/
3+
// 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/
34

45
// CHECK-FIXES: #include <algorithm>
56
// CHECK-FIXES-CPP23: #include <numeric>
67
// CHECK-FIXES: #include <ranges>
78

8-
#include "fake_std.h"
9+
#include "use-ranges/fake_std.h"
10+
#include "smart-ptr/unique_ptr.h"
911

1012
void Positives() {
1113
std::vector<int> I, J;
14+
std::vector<std::unique_ptr<int>> K;
15+
16+
// Expect to have no check messages
17+
std::find(K.begin(), K.end(), nullptr);
18+
19+
std::find(K.begin(), K.end(), std::unique_ptr<int>());
20+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
21+
// CHECK-FIXES: std::ranges::find(K, std::unique_ptr<int>());
22+
1223
std::find(I.begin(), I.end(), 0);
1324
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
1425
// CHECK-FIXES: std::ranges::find(I, 0);
@@ -76,6 +87,15 @@ void Positives() {
7687

7788
void Reverse(){
7889
std::vector<int> I, J;
90+
std::vector<std::unique_ptr<int>> K;
91+
92+
// Expect to have no check messages
93+
std::find(K.rbegin(), K.rend(), nullptr);
94+
95+
std::find(K.rbegin(), K.rend(), std::unique_ptr<int>());
96+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
97+
// CHECK-FIXES: std::ranges::find(std::ranges::reverse_view(K), std::unique_ptr<int>());
98+
7999
std::find(I.rbegin(), I.rend(), 0);
80100
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
81101
// CHECK-FIXES: std::ranges::find(std::ranges::reverse_view(I), 0);

0 commit comments

Comments
 (0)