Author: Victor Chernyakin Date: 2025-12-31T01:40:55-08:00 New Revision: def2e08365af3ce598fc61df06ae24d79a36057f
URL: https://github.com/llvm/llvm-project/commit/def2e08365af3ce598fc61df06ae24d79a36057f DIFF: https://github.com/llvm/llvm-project/commit/def2e08365af3ce598fc61df06ae24d79a36057f.diff LOG: [clang-tidy] Fix some false negatives in `bugprone-throw-keyword-missing` (#173748) Fixes #115540. Added: Modified: clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/bugprone/throw-keyword-missing.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp index 9781f0a5ac9de..7cf9696378c62 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp @@ -11,24 +11,29 @@ #include "clang/ASTMatchers/ASTMatchFinder.h" using namespace clang::ast_matchers; +using namespace clang::ast_matchers::internal; namespace clang::tidy::bugprone { void ThrowKeywordMissingCheck::registerMatchers(MatchFinder *Finder) { + const VariadicDynCastAllOfMatcher<Stmt, AttributedStmt> AttributedStmt; + // Matches an 'expression-statement', as defined in [stmt.expr]/1. + // Not to be confused with the similarly-named GNU extension, the + // statement expression. + const auto ExprStmt = [&](const Matcher<Expr> &InnerMatcher) { + return expr(hasParent(stmt(anyOf(doStmt(), whileStmt(), forStmt(), + compoundStmt(), ifStmt(), switchStmt(), + labelStmt(), AttributedStmt()))), + InnerMatcher); + }; + Finder->addMatcher( - cxxConstructExpr( - hasType(cxxRecordDecl(anyOf( + ExprStmt( + cxxConstructExpr(hasType(cxxRecordDecl(anyOf( matchesName("[Ee]xception|EXCEPTION"), hasAnyBase(hasType(hasCanonicalType(recordType(hasDeclaration( cxxRecordDecl(matchesName("[Ee]xception|EXCEPTION")) - .bind("base"))))))))), - unless(anyOf( - hasAncestor( - stmt(anyOf(cxxThrowExpr(), callExpr(), returnStmt()))), - hasAncestor(decl(anyOf(varDecl(), fieldDecl()))), - hasAncestor(expr(cxxNewExpr(hasAnyPlacementArg(anything())))), - allOf(hasAncestor(cxxConstructorDecl()), - unless(hasAncestor(cxxCatchStmt())))))) + .bind("base"))))))))))) .bind("temporary-exception-not-thrown"), this); } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 56e2b56249fa4..512cd29ed2715 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -432,7 +432,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 and adding a note on the base - class that triggered the warning. + class that triggered the warning. Also, fixed an issue where the check + wouldn't fire in constructors or (in certain contexts) lambdas. - Improved :doc:`bugprone-unchecked-optional-access <clang-tidy/checks/bugprone/unchecked-optional-access>` check by supporting 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 0ae51780ccc00..34326c4799e7b 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 @@ -49,7 +49,7 @@ struct RegularException { // -------------- -void stdExceptionNotTrownTest(int i) { +void stdExceptionNotThrownTest(int i) { if (i < 0) // CHECK-MESSAGES-DAG: :[[@LINE+1]]:5: warning: suspicious exception object created but not thrown; did you mean 'throw {{.*}}'? [bugprone-throw-keyword-missing] std::exception(); @@ -205,3 +205,24 @@ void placeMentNewTest() { alignas(RegularException) unsigned char expr[sizeof(RegularException)]; new (expr) RegularException{}; } + +void lambdaAsVariableInitializerTest() { + const auto var = [] { + // CHECK-MESSAGES: :[[@LINE+1]]:5: warning: suspicious exception + RegularException{0}; + }; +} + +void lambdaInReturnTest() { + return [] { + // CHECK-MESSAGES: :[[@LINE+1]]:5: warning: suspicious exception + RegularException{0}; + }(); +} + +struct ExceptionInConstructorTest { + ExceptionInConstructorTest() { + // CHECK-MESSAGES: :[[@LINE+1]]:5: warning: suspicious exception + RegularException{0}; + } +}; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
