[PATCH] D140434: readability-const-return-type: don't diagnose a template function returning T, even if sometimes instantiated with e.g. T = const int.

Andy Getz via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 20 14:42:18 PST 2022


suertreus created this revision.
suertreus added a reviewer: ymandel.
Herald added a subscriber: carlosgalvezp.
Herald added a reviewer: njames93.
Herald added a project: All.
suertreus requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

It's not really a readability problem since there's no `const` to read at the declaration site, and returning std::remove_const_t<T> instead usually only hurts readability.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140434

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
@@ -340,3 +340,18 @@
 __typeof__(const int) n21() {
   return 21;
 }
+
+template <typename T>
+struct n25 {
+  T foo() const { return 2; }
+};
+template struct n25<const int>;
+
+template <typename T>
+struct p41 {
+  const T foo() const { return 2; }
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: return type 'const
+  // CHECK-MESSAGES: [[@LINE-2]]:3: warning: return type 'const
+  // CHECK-FIXES: T foo() const { return 2; }
+};
+template struct p41<int>;
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
@@ -112,9 +112,10 @@
 void ConstReturnTypeCheck::registerMatchers(MatchFinder *Finder) {
   // Find all function definitions for which the return types are `const`
   // qualified, ignoring decltype types.
-  auto NonLocalConstType = qualType(
-      unless(isLocalConstQualified()),
-      anyOf(decltypeType(), autoType(), isTypeOfType(), isTypeOfExprType()));
+  auto NonLocalConstType =
+      qualType(unless(isLocalConstQualified()),
+               anyOf(decltypeType(), autoType(), isTypeOfType(),
+                     isTypeOfExprType(), substTemplateTypeParmType()));
   Finder->addMatcher(
       functionDecl(
           returns(allOf(isConstQualified(), unless(NonLocalConstType))),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140434.484386.patch
Type: text/x-patch
Size: 1752 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221220/75d90288/attachment.bin>


More information about the cfe-commits mailing list