diff --git a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp index 89eafb15f2652..cafb4a3e5f0e5 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp @@ -17,8 +17,10 @@ namespace clang::tidy::bugprone { void ThrowKeywordMissingCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxConstructExpr( - hasType(cxxRecordDecl( - isSameOrDerivedFrom(matchesName("[Ee]xception|EXCEPTION")))), + hasType(cxxRecordDecl(anyOf( + matchesName("[Ee]xception|EXCEPTION"), + hasAnyBase(hasType(hasCanonicalType(recordType(hasDeclaration( + cxxRecordDecl(matchesName("[Ee]xception|EXCEPTION")))))))))), unless(anyOf( hasAncestor( stmt(anyOf(cxxThrowExpr(), callExpr(), returnStmt()))), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index d13f83079e591..29a981ec585d0 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -260,6 +260,10 @@ Changes in existing checks namespace are treated as the tag or the data part of a user-defined tagged union respectively. +- Improved :doc:`bugprone-throw-keyword-missing + ` check by only considering + the canonical types of base classes as written. + - Improved :doc:`bugprone-unchecked-optional-access ` check by supporting ``NullableValue::makeValue`` and ``NullableValue::makeValueInplace`` to diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/throw-keyword-missing.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/throw-keyword-missing.cpp index bafd3d19b5a31..6ddaf246a354e 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/throw-keyword-missing.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/throw-keyword-missing.cpp @@ -32,8 +32,9 @@ struct runtime_error : public exception { } // namespace std -// The usage of this class should never emit a warning. +// The usage of these classes should never emit a warning. struct RegularClass {}; +struct RegularDerived : public RegularClass {}; // Class name contains the substring "exception", in certain cases using this class should emit a warning. struct RegularException { @@ -41,6 +42,8 @@ struct RegularException { // Constructors with a single argument are treated differently (cxxFunctionalCastExpr). RegularException(int) {} + + typedef RegularClass RegularAlias; }; // -------------- @@ -68,6 +71,10 @@ void regularClassNotThrownTest(int i) { RegularClass(); } +void regularClassWithAliasNotThrownTest(int i) { + RegularDerived(); +} + void regularClassThrownTest(int i) { if (i < 0) throw RegularClass();