[PATCH] D130665: [clang-tidy] Fix false negative in readability-convert-member-functions-to-static

Piotr Zegar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 24 07:35:48 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG67e05d380c22: [clang-tidy] Fix false negative in readability-convert-member-functions-to… (authored by njames93, committed by PiotrZSL).

Changed prior to commit:
  https://reviews.llvm.org/D130665?vs=448166&id=534211#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D130665/new/

https://reviews.llvm.org/D130665

Files:
  clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static.cpp
@@ -45,6 +45,24 @@
     static_field = 1;
   }
 
+  void static_nested() {
+    // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'static_nested' can be made static
+    // CHECK-FIXES: {{^}}  static void static_nested() {
+    struct Nested {
+      int Foo;
+      int getFoo() { return Foo; }
+    };
+  }
+
+  void write_nested() {
+    struct Nested {
+      int Foo;
+      int getFoo() { return Foo; }
+    };
+    // Ensure we still detect usages of `this` once we leave the nested class definition.
+    field = 1;
+  }
+
   static int already_static() { return static_field; }
 
   int already_const() const { return field; }
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -412,6 +412,10 @@
   ``std::array`` objects to default constructed ones. The behavior for this and
   other relevant classes can now be configured with a new option.
 
+- Fixed a false negative in :doc:`readability-convert-member-functions-to-static
+  <clang-tidy/checks/readability/convert-member-functions-to-static>` when a
+  nested class in a member function uses a ``this`` pointer.
+
 - Fixed reading `HungarianNotation.CString.*` options in
   :doc:`readability-identifier-naming
   <clang-tidy/checks/readability/identifier-naming>` check.
Index: clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
+++ clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStatic.cpp
@@ -61,6 +61,11 @@
       Used = true;
       return false; // Stop traversal.
     }
+
+    // If we enter a class declaration, don't traverse into it as any usages of
+    // `this` will correspond to the nested class.
+    bool TraverseCXXRecordDecl(CXXRecordDecl *RD) { return true; }
+
   } UsageOfThis;
 
   // TraverseStmt does not modify its argument.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130665.534211.patch
Type: text/x-patch
Size: 2429 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230624/d68460fc/attachment-0001.bin>


More information about the cfe-commits mailing list