-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-tidy] Relax readability-const-return-type #90560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PiotrZSL
merged 1 commit into
llvm:main
from
PiotrZSL:73270-readability-const-return-type-warning-lacks-context
May 2, 2024
Merged
[clang-tidy] Relax readability-const-return-type #90560
PiotrZSL
merged 1 commit into
llvm:main
from
PiotrZSL:73270-readability-const-return-type-warning-lacks-context
May 2, 2024
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 llvm#73270
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: Piotr Zegar (PiotrZSL) ChangesFrom 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 Full diff: https://github.com/llvm/llvm-project/pull/90560.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
index e92350632b556b..c13a8010c22210 100644
--- a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp
@@ -55,14 +55,6 @@ AST_MATCHER(QualType, isLocalConstQualified) {
return Node.isLocalConstQualified();
}
-AST_MATCHER(QualType, isTypeOfType) {
- return isa<TypeOfType>(Node.getTypePtr());
-}
-
-AST_MATCHER(QualType, isTypeOfExprType) {
- return isa<TypeOfExprType>(Node.getTypePtr());
-}
-
struct CheckResult {
// Source range of the relevant `const` token in the definition being checked.
CharSourceRange ConstRange;
@@ -110,16 +102,11 @@ 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(), substTemplateTypeParmType()));
Finder->addMatcher(
- functionDecl(
- returns(allOf(isConstQualified(), unless(NonLocalConstType))),
- anyOf(isDefinition(), cxxMethodDecl(isPure())),
- // Overridden functions are not actionable.
- unless(cxxMethodDecl(isOverride())))
+ functionDecl(returns(isLocalConstQualified()),
+ anyOf(isDefinition(), cxxMethodDecl(isPure())),
+ // Overridden functions are not actionable.
+ unless(cxxMethodDecl(isOverride())))
.bind("func"),
this);
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 3038d2b125f20d..d8bda2d8aea142 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -312,6 +312,10 @@ Changes in existing checks
<clang-tidy/checks/readability/avoid-return-with-void-value>` check by adding
fix-its.
+- Improved :doc:`readability-const-return-type
+ <clang-tidy/checks/readability/const-return-type>` check to eliminate false
+ positives when returning types with const not at the top level.
+
- Improved :doc:`readability-duplicate-include
<clang-tidy/checks/readability/duplicate-include>` check by excluding include
directives that form the filename using macro.
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 10b2858c9caa82..76a3555663b180 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
@@ -215,11 +215,9 @@ CREATE_FUNCTION();
using ty = const int;
ty p21() {}
-// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'ty' (aka 'const int') is
typedef const int ty2;
ty2 p22() {}
-// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'ty2' (aka 'const int') i
// Declaration uses a macro, while definition doesn't. In this case, we won't
// fix the declaration, and will instead issue a warning.
@@ -249,7 +247,6 @@ auto p27() -> int const { return 3; }
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'const int' is 'const'-qu
std::add_const<int>::type p28() { return 3; }
-// CHECK-MESSAGES: [[@LINE-1]]:1: warning: return type 'std::add_const<int>::typ
// p29, p30 are based on
// llvm/projects/test-suite/SingleSource/Benchmarks/Misc-C++-EH/spirit.cpp:
@@ -355,3 +352,20 @@ struct p41 {
// CHECK-FIXES: T foo() const { return 2; }
};
template struct p41<int>;
+
+namespace PR73270 {
+ template<typename K, typename V>
+ struct Pair {
+ using first_type = const K;
+ using second_type = V;
+ };
+
+ template<typename PairType>
+ typename PairType::first_type getFirst() {
+ return {};
+ }
+
+ void test() {
+ getFirst<Pair<int, int>>();
+ }
+}
|
SimplyDanny
approved these changes
Apr 30, 2024
HerrCai0907
approved these changes
Apr 30, 2024
5chmidti
approved these changes
May 1, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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