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

gehry via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 31 03:42:07 PST 2021


Sockke created this revision.
Sockke added a reviewer: aaron.ballman.
Herald added a subscriber: carlosgalvezp.
Sockke requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

It cannot match a `pure virtual function`. This patch fixes this behavior.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116439

Files:
  clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
  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-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-FIXES: int getC() { return 1; }
+};
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);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116439.396769.patch
Type: text/x-patch
Size: 1648 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211231/686da049/attachment.bin>


More information about the cfe-commits mailing list