Skip to content

Commit 1f1a417

Browse files
authored
[clang-tidy] Relax readability-const-return-type (#90560)
From now readability-const-return-type won't provide warnings for returning const types, where const is not on top level. In such case const there is a performance issue, but not a readability. Closes #73270
1 parent 889e60d commit 1f1a417

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@ AST_MATCHER(QualType, isLocalConstQualified) {
5555
return Node.isLocalConstQualified();
5656
}
5757

58-
AST_MATCHER(QualType, isTypeOfType) {
59-
return isa<TypeOfType>(Node.getTypePtr());
60-
}
61-
62-
AST_MATCHER(QualType, isTypeOfExprType) {
63-
return isa<TypeOfExprType>(Node.getTypePtr());
64-
}
65-
6658
struct CheckResult {
6759
// Source range of the relevant `const` token in the definition being checked.
6860
CharSourceRange ConstRange;
@@ -110,16 +102,11 @@ void ConstReturnTypeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
110102
void ConstReturnTypeCheck::registerMatchers(MatchFinder *Finder) {
111103
// Find all function definitions for which the return types are `const`
112104
// qualified, ignoring decltype types.
113-
auto NonLocalConstType =
114-
qualType(unless(isLocalConstQualified()),
115-
anyOf(decltypeType(), autoType(), isTypeOfType(),
116-
isTypeOfExprType(), substTemplateTypeParmType()));
117105
Finder->addMatcher(
118-
functionDecl(
119-
returns(allOf(isConstQualified(), unless(NonLocalConstType))),
120-
anyOf(isDefinition(), cxxMethodDecl(isPure())),
121-
// Overridden functions are not actionable.
122-
unless(cxxMethodDecl(isOverride())))
106+
functionDecl(returns(isLocalConstQualified()),
107+
anyOf(isDefinition(), cxxMethodDecl(isPure())),
108+
// Overridden functions are not actionable.
109+
unless(cxxMethodDecl(isOverride())))
123110
.bind("func"),
124111
this);
125112
}

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ Changes in existing checks
318318
<clang-tidy/checks/readability/avoid-return-with-void-value>` check by adding
319319
fix-its.
320320

321+
- Improved :doc:`readability-const-return-type
322+
<clang-tidy/checks/readability/const-return-type>` check to eliminate false
323+
positives when returning types with const not at the top level.
324+
321325
- Improved :doc:`readability-duplicate-include
322326
<clang-tidy/checks/readability/duplicate-include>` check by excluding include
323327
directives that form the filename using macro.

clang-tools-extra/test/clang-tidy/checkers/readability/const-return-type.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,9 @@ CREATE_FUNCTION();
215215

216216
using ty = const int;
217217
ty p21() {}
218-
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'ty' (aka 'const int') is
219218

220219
typedef const int ty2;
221220
ty2 p22() {}
222-
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'ty2' (aka 'const int') i
223221

224222
// Declaration uses a macro, while definition doesn't. In this case, we won't
225223
// fix the declaration, and will instead issue a warning.
@@ -249,7 +247,6 @@ auto p27() -> int const { return 3; }
249247
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'const int' is 'const'-qu
250248

251249
std::add_const<int>::type p28() { return 3; }
252-
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'std::add_const<int>::typ
253250

254251
// p29, p30 are based on
255252
// llvm/projects/test-suite/SingleSource/Benchmarks/Misc-C++-EH/spirit.cpp:
@@ -355,3 +352,20 @@ struct p41 {
355352
// CHECK-FIXES: T foo() const { return 2; }
356353
};
357354
template struct p41<int>;
355+
356+
namespace PR73270 {
357+
template<typename K, typename V>
358+
struct Pair {
359+
using first_type = const K;
360+
using second_type = V;
361+
};
362+
363+
template<typename PairType>
364+
typename PairType::first_type getFirst() {
365+
return {};
366+
}
367+
368+
void test() {
369+
getFirst<Pair<int, int>>();
370+
}
371+
}

0 commit comments

Comments
 (0)