From ac4a86af0ae262751d4d38b65148aa6d999dd925 Mon Sep 17 00:00:00 2001 From: Andrey Karlov Date: Tue, 30 Sep 2025 16:37:45 +0300 Subject: [PATCH 1/2] [clang-tidy]: Ignore empty `catch` blocks in destructors in `bugprone-empty-catch` check --- .../clang-tidy/bugprone/EmptyCatchCheck.cpp | 1 + clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ .../test/clang-tidy/checkers/bugprone/empty-catch.cpp | 9 +++++++++ 3 files changed, 14 insertions(+) diff --git a/clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp index eebab847d1070..48dc3bdfdf49e 100644 --- a/clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp @@ -90,6 +90,7 @@ void EmptyCatchCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxCatchStmt(unless(isExpansionInSystemHeader()), unless(isInMacro()), unless(hasCaughtType(IgnoredExceptionType)), + unless(hasAncestor(cxxDestructorDecl())), hasHandler(compoundStmt( statementCountIs(0), unless(hasAnyTextFromList(IgnoreCatchWithKeywords))))) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index c3a6d2f9b2890..426c97225c0e1 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -244,6 +244,10 @@ Changes in existing checks correcting a spelling mistake on its option ``NamePrefixSuffixSilenceDissimilarityTreshold``. +- Improved :doc:`bugprone-empty-catch + ` check by allowing empty + ``catch`` blocks in destructors. + - Improved :doc:`bugprone-exception-escape ` check's handling of lambdas: exceptions from captures are now diagnosed, exceptions in the bodies of diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/empty-catch.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/empty-catch.cpp index 8ab38229b6dbf..1319496269d86 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/empty-catch.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/empty-catch.cpp @@ -65,3 +65,12 @@ void functionWithComment2() { // @IGNORE: relax its safe } } + +struct StructWithEmptyCatchInDestructor { + ~StructWithEmptyCatchInDestructor() { + try { + } + catch (...) { + } + } +}; From 2a789f3e86c1ae878eedf7f17acc7ef8214cbfee Mon Sep 17 00:00:00 2001 From: Andrey Karlov Date: Sat, 4 Oct 2025 14:19:27 +0300 Subject: [PATCH 2/2] Update clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp Co-authored-by: Victor Chernyakin --- clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp index 48dc3bdfdf49e..16264b9af1ae0 100644 --- a/clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp @@ -90,7 +90,7 @@ void EmptyCatchCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxCatchStmt(unless(isExpansionInSystemHeader()), unless(isInMacro()), unless(hasCaughtType(IgnoredExceptionType)), - unless(hasAncestor(cxxDestructorDecl())), + unless(hasDeclContext(cxxDestructorDecl())), hasHandler(compoundStmt( statementCountIs(0), unless(hasAnyTextFromList(IgnoreCatchWithKeywords)))))