Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ void ThrowKeywordMissingCheck::registerMatchers(MatchFinder *Finder) {
hasType(cxxRecordDecl(anyOf(
matchesName("[Ee]xception|EXCEPTION"),
hasAnyBase(hasType(hasCanonicalType(recordType(hasDeclaration(
cxxRecordDecl(matchesName("[Ee]xception|EXCEPTION")))))))))),
cxxRecordDecl(matchesName("[Ee]xception|EXCEPTION"))
.bind("base"))))))))),
unless(anyOf(
hasAncestor(
stmt(anyOf(cxxThrowExpr(), callExpr(), returnStmt()))),
Expand All @@ -39,6 +40,11 @@ void ThrowKeywordMissingCheck::check(const MatchFinder::MatchResult &Result) {
diag(TemporaryExpr->getBeginLoc(), "suspicious exception object created but "
"not thrown; did you mean 'throw %0'?")
<< TemporaryExpr->getType().getBaseTypeIdentifier()->getName();

if (const auto *BaseDecl = Result.Nodes.getNodeAs<Decl>("base"))
diag(BaseDecl->getLocation(),
"object type inherits from base class declared here",
DiagnosticIDs::Note);
}

} // namespace clang::tidy::bugprone
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ Changes in existing checks

- Improved :doc:`bugprone-throw-keyword-missing
<clang-tidy/checks/bugprone/throw-keyword-missing>` check by only considering
the canonical types of base classes as written.
the canonical types of base classes as written and adding a note on the base
class that triggered the warning.

- Improved :doc:`bugprone-unchecked-optional-access
<clang-tidy/checks/bugprone/unchecked-optional-access>` check by supporting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;

// std::exception and std::runtime_error declaration.
// CHECK-MESSAGES-DAG: [[#EXCEPTION_LINE:@LINE + 1]]:8
struct exception {
exception();
exception(const exception &other);
Expand Down Expand Up @@ -50,12 +51,13 @@ struct RegularException {

void stdExceptionNotTrownTest(int i) {
if (i < 0)
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: suspicious exception object created but not thrown; did you mean 'throw {{.*}}'? [bugprone-throw-keyword-missing]
// CHECK-MESSAGES-DAG: :[[@LINE+1]]:5: warning: suspicious exception object created but not thrown; did you mean 'throw {{.*}}'? [bugprone-throw-keyword-missing]
std::exception();

if (i > 0)
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: suspicious exception
// CHECK-MESSAGES-DAG: :[[@LINE+1]]:5: warning: suspicious exception
std::runtime_error("Unexpected argument");
// CHECK-MESSAGES: note: object type inherits from base class declared here
}

void stdExceptionThrownTest(int i) {
Expand Down Expand Up @@ -181,6 +183,7 @@ class RegularError : public ERROR_BASE {};
void typedefTest() {
// CHECK-MESSAGES: :[[@LINE+1]]:3: warning: suspicious exception
RegularError();
// CHECK-MESSAGES: :[[#EXCEPTION_LINE]]:8: note: object type inherits from base class declared here
}

struct ExceptionRAII {
Expand Down
Loading