diff options
author | Sockke <liuke.gehry@bytedance.com> | 2022-03-01 20:54:10 +0800 |
---|---|---|
committer | Sockke <liuke.gehry@bytedance.com> | 2022-03-01 20:55:28 +0800 |
commit | ba54ebeb5eba0f63de8ce2d73a85e9bf508008f6 (patch) | |
tree | 9f042a54d8cea447de7dd7f90fbade455885e8cd | |
parent | ab49dce01f211fd80f76f449035d771f5e2720b9 (diff) | |
download | llvm-ba54ebeb5eba0f63de8ce2d73a85e9bf508008f6.zip llvm-ba54ebeb5eba0f63de8ce2d73a85e9bf508008f6.tar.gz llvm-ba54ebeb5eba0f63de8ce2d73a85e9bf508008f6.tar.bz2 |
[clang-tidy] Fix `readability-const-return-type` for pure virtual function.
It cannot match a `pure virtual function`. This patch fixes this behavior.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D116439
3 files changed, 27 insertions, 1 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp index 1647cb1..814d3f2 100644 --- a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp @@ -97,7 +97,9 @@ void ConstReturnTypeCheck::registerMatchers(MatchFinder *Finder) { // 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); } @@ -115,6 +117,12 @@ void ConstReturnTypeCheck::check(const MatchFinder::MatchResult &Result) { << Def->getReturnType(); if (CR.ConstRange.isValid()) Diagnostic << CR.ConstRange; + + // Do not propose fixes for virtual function. + const auto *Method = dyn_cast<CXXMethodDecl>(Def); + if (Method && Method->isVirtual()) + return; + for (auto &Hint : CR.Hints) Diagnostic << Hint; } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 103f337..551e36d 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -112,6 +112,10 @@ Changes in existing checks - Fixed a false positive in :doc:`readability-non-const-parameter <clang-tidy/checks/readability-non-const-parameter>` when the parameter is referenced by an lvalue +- Fixed a crash in :doc:`readability-const-return-type + <clang-tidy/checks/readability-const-return-type>` when a pure virtual function + overrided has a const return type. Removed the fix for a virtual function. + Removed checks ^^^^^^^^^^^^^^ 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 f801b18..70da965 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 @@ -271,3 +271,17 @@ const int n14(); 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-NOT-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-NOT-FIXES: int getC() { return 1; } +}; |