[PATCH] D116439: [clang-tidy] Fix `readability-const-return-type` for pure virtual function.

gehry via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 27 19:01:13 PST 2022


Sockke updated this revision to Diff 411722.
Sockke added a comment.

Added a release note.


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

https://reviews.llvm.org/D116439

Files:
  clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/test/clang-tidy/checkers/readability-const-return-type.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/readability-const-return-type.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability-const-return-type.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-const-return-type.cpp
@@ -271,3 +271,17 @@
 
 int **const * n_multiple_ptr();
 int *const & n_pointer_ref();
+
+class PVBase {
+public:
+  virtual const int getC() = 0;
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: return type 'const int' is 'const'-qualified at the top level, which may reduce code readability without improving const correctness
+  // CHECK-NOT-FIXES: virtual int getC() = 0;
+};
+
+class PVDerive : public PVBase {
+public:
+  const int getC() { return 1; }
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: return type 'const int' is 'const'-qualified at the top level, which may reduce code readability without improving const correctness
+  // CHECK-NOT-FIXES: int getC() { return 1; }
+};
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -116,6 +116,10 @@
   <clang-tidy/checks/readability-suspicious-call-argument>` related to passing
   arguments that refer to program elements without a trivial identifier.
 
+- Fixed a crash in :doc:`readability-const-return-type
+  <clang-tidy/checks/readability-const-return-type>` when a pure virtual function
+  overrided has a const return type. Removed the fix for a virtual function.
+
 Removed checks
 ^^^^^^^^^^^^^^
 
Index: clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
@@ -97,7 +97,9 @@
   // Find all function definitions for which the return types are `const`
   // qualified.
   Finder->addMatcher(
-      functionDecl(returns(isConstQualified()), isDefinition()).bind("func"),
+      functionDecl(returns(isConstQualified()),
+                   anyOf(isDefinition(), cxxMethodDecl(isPure())))
+          .bind("func"),
       this);
 }
 
@@ -115,6 +117,12 @@
         << Def->getReturnType();
     if (CR.ConstRange.isValid())
       Diagnostic << CR.ConstRange;
+
+    // Do not propose fixes for virtual function.
+    const auto *Method = dyn_cast<CXXMethodDecl>(Def);
+    if (Method && Method->isVirtual())
+      return;
+
     for (auto &Hint : CR.Hints)
       Diagnostic << Hint;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116439.411722.patch
Type: text/x-patch
Size: 2645 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220228/a9ba8335/attachment.bin>


More information about the cfe-commits mailing list