Skip to content

Commit 7b73f5d

Browse files
committed
[clang-tidy] Fix invalid fixit from modernize-use-ranges for nullptr used with std::unique_ptr
1 parent 2e3729b commit 7b73f5d

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ void UseRangesCheck::check(const MatchFinder::MatchResult &Result) {
215215
const auto *Call = Result.Nodes.getNodeAs<CallExpr>(Buffer);
216216
if (!Call)
217217
continue;
218+
if (Function->getName() == "find") {
219+
unsigned ValueArgIndex = 2;
220+
if (Call->getNumArgs() <= ValueArgIndex)
221+
continue;
222+
const Expr *ValueExpr =
223+
Call->getArg(ValueArgIndex)->IgnoreParenImpCasts();
224+
if (isa<CXXNullPtrLiteralExpr>(ValueExpr)) {
225+
return;
226+
}
227+
}
218228
auto Diag = createDiag(*Call);
219229
if (auto ReplaceName = Replacer->getReplaceName(*Function))
220230
Diag << FixItHint::CreateReplacement(Call->getCallee()->getSourceRange(),

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
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+
15+
// Expect to have no check messages
16+
std::find(I.begin(), I.end(), nullptr);
17+
18+
std::find(I.begin(), I.end(), std::unique_ptr<int>());
19+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
20+
// CHECK-FIXES: std::ranges::find(I, std::unique_ptr<int>());
21+
1222
std::find(I.begin(), I.end(), 0);
1323
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
1424
// CHECK-FIXES: std::ranges::find(I, 0);
@@ -76,6 +86,14 @@ void Positives() {
7686

7787
void Reverse(){
7888
std::vector<int> I, J;
89+
90+
// Expect to have no check messages
91+
std::find(I.rbegin(), I.rend(), nullptr);
92+
93+
std::find(I.rbegin(), I.rend(), std::unique_ptr<int>());
94+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
95+
// CHECK-FIXES: std::ranges::find(std::ranges::reverse_view(I), std::unique_ptr<int>());
96+
7997
std::find(I.rbegin(), I.rend(), 0);
8098
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
8199
// CHECK-FIXES: std::ranges::find(std::ranges::reverse_view(I), 0);
@@ -112,3 +130,4 @@ void Negatives() {
112130
// Pathological, but probably shouldn't diagnose this
113131
std::rotate(I.begin(), I.end(), I.end() + 0);
114132
}
133+

0 commit comments

Comments
 (0)