Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fb2a406

Browse filesBrowse files
committed
[clang-tidy] Fix false negative modernize-use-ranges when using getter function
1 parent f01b56f commit fb2a406
Copy full SHA for fb2a406

File tree

Expand file treeCollapse file tree

3 files changed

+42
-3
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+42
-3
lines changed

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

Copy file name to clipboardExpand all lines: clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ static std::string getFullPrefix(ArrayRef<UseRangesCheck::Indexes> Signature) {
5151
namespace {
5252

5353
AST_MATCHER(Expr, hasSideEffects) {
54-
return Node.HasSideEffects(Finder->getASTContext());
54+
bool CheckArg = true;
55+
if (const CXXMemberCallExpr *Call = dyn_cast<CXXMemberCallExpr>(&Node)) {
56+
if (Call->isLValue() && Call->getMethodDecl()->isConst()) {
57+
CheckArg = false;
58+
}
59+
}
60+
return Node.HasSideEffects(Finder->getASTContext(), CheckArg);
5561
}
5662
} // namespace
5763

‎clang-tools-extra/docs/ReleaseNotes.rst

Copy file name to clipboardExpand all lines: clang-tools-extra/docs/ReleaseNotes.rst
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ Changes in existing checks
188188
tolerating fix-it breaking compilation when functions is used as pointers
189189
to avoid matching usage of functions within the current compilation unit.
190190

191+
- Improved :doc:`modernize-use-ranges
192+
<clang-tidy/checks/modernize/use-ranges>` check by correctly recognizes
193+
const member functions returning lvalues as side-effect-free, preventing
194+
missed transformations.
195+
191196
Removed checks
192197
^^^^^^^^^^^^^^
193198

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

Copy file name to clipboardExpand all lines: clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
+30-2Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99
#include "use-ranges/fake_std.h"
1010
#include "smart-ptr/unique_ptr.h"
1111

12-
void Positives() {
12+
struct S {
13+
std::vector<int> v;
14+
15+
const std::vector<int>& get() const { return v; }
16+
std::vector<int>& getMutable() { return v; }
17+
std::vector<int> getVal() const { return v; }
18+
};
19+
20+
void Positives(S& s) {
1321
std::vector<int> I, J;
1422
std::vector<std::unique_ptr<int>> K;
1523

@@ -83,9 +91,19 @@ void Positives() {
8391
my_std::find(I.begin(), I.end(), 6);
8492
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
8593
// CHECK-FIXES: std::ranges::find(I, 6);
94+
95+
std::find(s.get().begin(), s.get().end(), 0);
96+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
97+
// CHECK-FIXES: std::ranges::find(s.get(), 0);
98+
99+
// Return non-const function, should not generate message
100+
std::find(s.getMutable().begin(), s.getMutable().end(), 0);
101+
102+
// Return value, should not generate message
103+
std::find(s.getVal().begin(), s.getVal().end(), 0);
86104
}
87105

88-
void Reverse(){
106+
void Reverse(S& s){
89107
std::vector<int> I, J;
90108
std::vector<std::unique_ptr<int>> K;
91109

@@ -107,6 +125,16 @@ void Reverse(){
107125
std::equal(I.begin(), I.end(), std::crbegin(J), std::crend(J));
108126
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
109127
// CHECK-FIXES: std::ranges::equal(I, std::ranges::reverse_view(J));
128+
129+
std::find(s.get().rbegin(), s.get().rend(), 0);
130+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
131+
// CHECK-FIXES: std::ranges::find(std::ranges::reverse_view(s.get()), 0);
132+
133+
// Return non-const function, should not generate message
134+
std::find(s.getMutable().rbegin(), s.getMutable().rend(), 0);
135+
136+
// Return value, should not generate message
137+
std::find(s.getVal().rbegin(), s.getVal().rend(), 0);
110138
}
111139

112140
void Negatives() {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.