Skip to content

Commit 308a236

Browse files
authoredMar 7, 2024
[clang-tidy] isOnlyUsedAsConst: Handle static method calls. (llvm#84005)
... using method syntax: ``` struct S { static void f() }; void DoIt(S& s) { s.f(); // Does not mutate `s` through the `this` parameter. } ```
1 parent 57a3373 commit 308a236

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed
 

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,16 @@ AST_MATCHER_P(DeclRefExpr, doesNotMutateObject, int, Indirections) {
155155
if (const auto *const Member = dyn_cast<MemberExpr>(P)) {
156156
if (const auto *const Method =
157157
dyn_cast<CXXMethodDecl>(Member->getMemberDecl())) {
158-
if (!Method->isConst()) {
159-
// The method can mutate our variable.
160-
return false;
158+
if (Method->isConst() || Method->isStatic()) {
159+
// The method call cannot mutate our variable.
160+
continue;
161161
}
162-
continue;
162+
return false;
163163
}
164164
Stack.emplace_back(Member, 0);
165165
continue;
166166
}
167+
167168
if (const auto *const Op = dyn_cast<UnaryOperator>(P)) {
168169
switch (Op->getOpcode()) {
169170
case UO_AddrOf:

‎clang-tools-extra/unittests/clang-tidy/DeclRefExprUtilsTest.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ template <int Indirections> void RunTest(StringRef Snippet) {
5151
void constMethod() const;
5252
void nonConstMethod();
5353
54+
static void staticMethod();
55+
5456
void operator()(ConstTag) const;
5557
void operator()(NonConstTag);
5658
@@ -109,10 +111,12 @@ TEST(ConstReferenceDeclRefExprsTest, ConstValueVar) {
109111
useConstPtr(&/*const*/target);
110112
useConstPtrConstRef(&/*const*/target);
111113
/*const*/target.constMethod();
114+
/*const*/target.staticMethod();
112115
/*const*/target(ConstTag{});
113116
/*const*/target[42];
114117
useConstRef((/*const*/target));
115118
(/*const*/target).constMethod();
119+
/*const*/target.staticMethod();
116120
(void)(/*const*/target == /*const*/target);
117121
(void)/*const*/target;
118122
(void)&/*const*/target;
@@ -140,6 +144,7 @@ TEST(ConstReferenceDeclRefExprsTest, ConstRefVar) {
140144
useConstPtr(&/*const*/target);
141145
useConstPtrConstRef(&/*const*/target);
142146
/*const*/target.constMethod();
147+
/*const*/target.staticMethod();
143148
/*const*/target(ConstTag{});
144149
/*const*/target[42];
145150
useConstRef((/*const*/target));
@@ -179,6 +184,7 @@ TEST(ConstReferenceDeclRefExprsTest, ValueVar) {
179184
useConstPtr(&/*const*/target);
180185
useConstPtrConstRef(&/*const*/target);
181186
/*const*/target.constMethod();
187+
/*const*/target.staticMethod();
182188
target.nonConstMethod();
183189
/*const*/target(ConstTag{});
184190
target[42];
@@ -218,6 +224,7 @@ TEST(ConstReferenceDeclRefExprsTest, RefVar) {
218224
useConstPtr(&/*const*/target);
219225
useConstPtrConstRef(&/*const*/target);
220226
/*const*/target.constMethod();
227+
/*const*/target.staticMethod();
221228
target.nonConstMethod();
222229
/*const*/target(ConstTag{});
223230
target[42];
@@ -256,6 +263,7 @@ TEST(ConstReferenceDeclRefExprsTest, PtrVar) {
256263
useConstPtrConstRef(/*const*/target);
257264
usePtrConstPtr(&target);
258265
/*const*/target->constMethod();
266+
/*const*/target->staticMethod();
259267
target->nonConstMethod();
260268
(*/*const*/target)(ConstTag{});
261269
(*target)[42];
@@ -292,6 +300,7 @@ TEST(ConstReferenceDeclRefExprsTest, ConstPtrVar) {
292300
useConstPtrConstPtr(&/*const*/target);
293301
useConstPtrConstRef(/*const*/target);
294302
/*const*/target->constMethod();
303+
/*const*/target->staticMethod();
295304
(*/*const*/target)(ConstTag{});
296305
(*/*const*/target)[42];
297306
/*const*/target->operator[](42);

0 commit comments

Comments
 (0)
Please sign in to comment.