Skip to content

[clang-tidy] isOnlyUsedAsConst: Handle static method calls. #84005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2024

Conversation

legrosbuffle
Copy link
Contributor

... using method syntax:

struct S {
  static void f()
};

void DoIt(S& s) {
  s.f();  // Does not mutate `s` through the `this` parameter.
}

... using method syntax:

```
struct S {
  static void f()
};

void DoIt(S& s) {
  s.f();  // Does not mutate `s` through the `this` parameter.
}
```
@llvmbot
Copy link
Member

llvmbot commented Mar 5, 2024

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: Clement Courbet (legrosbuffle)

Changes

... using method syntax:

struct S {
  static void f()
};

void DoIt(S& s) {
  s.f();  // Does not mutate `s` through the `this` parameter.
}

Full diff: https://github.com/llvm/llvm-project/pull/84005.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp (+5-4)
  • (modified) clang-tools-extra/unittests/clang-tidy/DeclRefExprUtilsTest.cpp (+9)
diff --git a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
index f0ffa517047b27..a48e45e1356813 100644
--- a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
+++ b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
@@ -155,15 +155,16 @@ AST_MATCHER_P(DeclRefExpr, doesNotMutateObject, int, Indirections) {
       if (const auto *const Member = dyn_cast<MemberExpr>(P)) {
         if (const auto *const Method =
                 dyn_cast<CXXMethodDecl>(Member->getMemberDecl())) {
-          if (!Method->isConst()) {
-            // The method can mutate our variable.
-            return false;
+          if (Method->isConst() || Method->isStatic()) {
+            // The method call cannot mutate our variable.
+            continue;
           }
-          continue;
+          return false;
         }
         Stack.emplace_back(Member, 0);
         continue;
       }
+
       if (const auto *const Op = dyn_cast<UnaryOperator>(P)) {
         switch (Op->getOpcode()) {
         case UO_AddrOf:
diff --git a/clang-tools-extra/unittests/clang-tidy/DeclRefExprUtilsTest.cpp b/clang-tools-extra/unittests/clang-tidy/DeclRefExprUtilsTest.cpp
index 4c9e81ea0f61ac..3d9f51e2e17b09 100644
--- a/clang-tools-extra/unittests/clang-tidy/DeclRefExprUtilsTest.cpp
+++ b/clang-tools-extra/unittests/clang-tidy/DeclRefExprUtilsTest.cpp
@@ -51,6 +51,8 @@ template <int Indirections> void RunTest(StringRef Snippet) {
       void constMethod() const;
       void nonConstMethod();
 
+      static void staticMethod();
+
       void operator()(ConstTag) const;
       void operator()(NonConstTag);
 
@@ -109,10 +111,12 @@ TEST(ConstReferenceDeclRefExprsTest, ConstValueVar) {
       useConstPtr(&/*const*/target);
       useConstPtrConstRef(&/*const*/target);
       /*const*/target.constMethod();
+      /*const*/target.staticMethod();
       /*const*/target(ConstTag{});
       /*const*/target[42];
       useConstRef((/*const*/target));
       (/*const*/target).constMethod();
+      /*const*/target.staticMethod();
       (void)(/*const*/target == /*const*/target);
       (void)/*const*/target;
       (void)&/*const*/target;
@@ -140,6 +144,7 @@ TEST(ConstReferenceDeclRefExprsTest, ConstRefVar) {
       useConstPtr(&/*const*/target);
       useConstPtrConstRef(&/*const*/target);
       /*const*/target.constMethod();
+      /*const*/target.staticMethod();
       /*const*/target(ConstTag{});
       /*const*/target[42];
       useConstRef((/*const*/target));
@@ -179,6 +184,7 @@ TEST(ConstReferenceDeclRefExprsTest, ValueVar) {
       useConstPtr(&/*const*/target);
       useConstPtrConstRef(&/*const*/target);
       /*const*/target.constMethod();
+      /*const*/target.staticMethod();
       target.nonConstMethod();
       /*const*/target(ConstTag{});
       target[42];
@@ -218,6 +224,7 @@ TEST(ConstReferenceDeclRefExprsTest, RefVar) {
       useConstPtr(&/*const*/target);
       useConstPtrConstRef(&/*const*/target);
       /*const*/target.constMethod();
+      /*const*/target.staticMethod();
       target.nonConstMethod();
       /*const*/target(ConstTag{});
       target[42];
@@ -256,6 +263,7 @@ TEST(ConstReferenceDeclRefExprsTest, PtrVar) {
       useConstPtrConstRef(/*const*/target);
       usePtrConstPtr(&target);
       /*const*/target->constMethod();
+      /*const*/target->staticMethod();
       target->nonConstMethod();
       (*/*const*/target)(ConstTag{});
       (*target)[42];
@@ -292,6 +300,7 @@ TEST(ConstReferenceDeclRefExprsTest, ConstPtrVar) {
       useConstPtrConstPtr(&/*const*/target);
       useConstPtrConstRef(/*const*/target);
       /*const*/target->constMethod();
+      /*const*/target->staticMethod();
       (*/*const*/target)(ConstTag{});
       (*/*const*/target)[42];
       /*const*/target->operator[](42);

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, verify if some release notes shouldn't be updated.

@legrosbuffle
Copy link
Contributor Author

LGTM, verify if some release notes shouldn't be updated.

This is very minor, I've only seen a few instances in the Google codebase, but it was trivial enough to include.

@PiotrZSL PiotrZSL merged commit 308a236 into llvm:main Mar 7, 2024
@PiotrZSL
Copy link
Member

PiotrZSL commented Mar 7, 2024

Ok, lets merge it then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants