-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy]: Ignore empty catch
blocks in destructors in bugprone-empty-catch
check
#161379
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
base: main
Are you sure you want to change the base?
Conversation
…-empty-catch` check
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: Andrey Karlov (negativ) ChangesWhile flagging empty The Rationale:
Proposal:Skip checks for Full diff: https://github.com/llvm/llvm-project/pull/161379.diff 3 Files Affected:
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
+ <clang-tidy/checks/bugprone/empty-catch>` check by allowing empty
+ ``catch`` blocks in destructors.
+
- Improved :doc:`bugprone-exception-escape
<clang-tidy/checks/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 (...) {
+ }
+ }
+};
|
I think this should be an option instead. |
That's a fair point, but from my perspective, the logic should actually be inverted. Instead of enabling this check by default and requiring users to disable it for destructors, the check should be disabled for destructors by default, with an option to turn it on if a user explicitly needs it. My reasoning is that an empty catch block inside a destructor is almost always a conscious and deliberate decision. The developer who wrote it likely had a very strong reason - for example, the valid concern that even a logging function could IMHO, destructors are critical code paths that are already written with heightened attention to their reliability, so we should trust the developer's intent by default in this specific context. |
There is a partial consensus in clang-tidy reviewers (at least carlosgalvezp and me) that we should make checks strict by default with options to be less strict. I hope @PiotrZSL would take a look at this PR since he was the author of the check. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also of the opinion that being stricter is the much better default. I think we are also quite consistent with it. Some checks have some matching disabled that would technically be more strict, and this could be said about this option as well but matching empty catches in destructors is the better default IMO.
Co-authored-by: Victor Chernyakin <chernyakin.victor.j@outlook.com>
You can test this locally with the following command:git diff -U0 origin/main...HEAD -- clang-tools-extra/clang-tidy/bugprone/EmptyCatchCheck.cpp |
python3 clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py \
-path build -p1 -quiet View the output from clang-tidy here.
|
While flagging empty
catch
blocks is generally a great rule, it produces false positives for destructors, where this pattern is often the only correct implementation.The Rationale:
std::terminate
.try...catch
block.std::bad_alloc
), "swallowing" the exception is the standard/safest approach.Proposal:
Skip checks for
catch
blocks within destructors.