Author: Tom Date: 2026-03-23T17:28:41+08:00 New Revision: bb86440c60a5c0e993c8dcdfded75d15881be294
URL: https://github.com/llvm/llvm-project/commit/bb86440c60a5c0e993c8dcdfded75d15881be294 DIFF: https://github.com/llvm/llvm-project/commit/bb86440c60a5c0e993c8dcdfded75d15881be294.diff LOG: [clang-tidy] Correctly ignore function templates in derived-method-shadowing-base-method (#185741) (#185875) This commit fixes a false positive in the derived-method-shadowin-base-method clang-tidy check, as described in [ticket 185741](https://github.com/llvm/llvm-project/issues/185741) Fixes #185741 --------- Co-authored-by: Tom James <[email protected]> Co-authored-by: Zeyi Xu <[email protected]> Added: Modified: clang-tools-extra/clang-tidy/bugprone/DerivedMethodShadowingBaseMethodCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/bugprone/derived-method-shadowing-base-method.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/bugprone/DerivedMethodShadowingBaseMethodCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/DerivedMethodShadowingBaseMethodCheck.cpp index d50569fa70851..904685cb8416d 100644 --- a/clang-tools-extra/clang-tidy/bugprone/DerivedMethodShadowingBaseMethodCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/DerivedMethodShadowingBaseMethodCheck.cpp @@ -81,6 +81,10 @@ AST_MATCHER(CXXMethodDecl, nameCollidesWithMethodInBase) { // similar matchers are used elsewhere in LLVM AST_MATCHER(CXXMethodDecl, isOutOfLine) { return Node.isOutOfLine(); } +AST_MATCHER(CXXMethodDecl, isTemplate) { + return Node.getDescribedFunctionTemplate() != nullptr; +} + } // namespace DerivedMethodShadowingBaseMethodCheck::DerivedMethodShadowingBaseMethodCheck( @@ -94,8 +98,8 @@ void DerivedMethodShadowingBaseMethodCheck::registerMatchers( unless(anyOf(isOutOfLine(), isStaticStorageClass(), isImplicit(), cxxConstructorDecl(), isOverride(), isPrivate(), // isFinal(), //included with isOverride, - // Templates are not handled yet - ast_matchers::isTemplateInstantiation(), + // TODO: Templates are not handled yet + isTemplate(), ast_matchers::isTemplateInstantiation(), ast_matchers::isExplicitTemplateSpecialization())), ofClass(cxxRecordDecl(isDerivedFrom(cxxRecordDecl())) .bind("derived_class")), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index daf635bf625e7..a346fee4ea33c 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -204,6 +204,10 @@ Changes in existing checks <clang-tidy/checks/bugprone/casting-through-void>` check by running only on C++ files because suggested ``reinterpret_cast`` is not available in pure C. +- Improved :doc:`bugprone-derived-method-shadowing-base-method + <clang-tidy/checks/bugprone/derived-method-shadowing-base-method>` check by + correctly ignoring function templates. + - Improved :doc:`bugprone-exception-escape <clang-tidy/checks/bugprone/exception-escape>` check by adding `TreatFunctionsWithoutSpecificationAsThrowing` option to support reporting diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/derived-method-shadowing-base-method.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/derived-method-shadowing-base-method.cpp index c22598d84d1b2..c615646251470 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/derived-method-shadowing-base-method.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/derived-method-shadowing-base-method.cpp @@ -137,3 +137,10 @@ class Q : public Base void methodWithArg(MyInt *I); void methodWithArg(MyInt const* I); }; + +class R: public Base +{ +public: + template <typename T> + Base* getThis(); +}; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
