[clang-tools-extra] 9a11b23 - readability-const-return-type: don't diagnose a template function returning T, even if sometimes instantiated with e.g. T = const int.
Yitzhak Mandelbaum via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 23 14:08:51 PST 2023
Author: Andy Getzendanner
Date: 2023-01-23T22:08:12Z
New Revision: 9a11b23c0238ebfa0210ce89dee97f68ae3dfbd4
URL: https://github.com/llvm/llvm-project/commit/9a11b23c0238ebfa0210ce89dee97f68ae3dfbd4
DIFF: https://github.com/llvm/llvm-project/commit/9a11b23c0238ebfa0210ce89dee97f68ae3dfbd4.diff
LOG: readability-const-return-type: don't diagnose a template function returning T, even if sometimes instantiated with e.g. T = const int.
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.
Reviewed By: ymandel
Differential Revision: https://reviews.llvm.org/D140434
Added:
Modified:
clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
index b63c25265d92..e92350632b55 100644
--- a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
@@ -110,9 +110,10 @@ void ConstReturnTypeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
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))),
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type.cpp
index 144dc00fcfea..5577060c402a 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type.cpp
@@ -340,3 +340,18 @@ __typeof__(n17i) n20() {
__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>;
More information about the cfe-commits
mailing list