Skip to content

Conversation

@Mr-Anyone
Copy link
Contributor

@Mr-Anyone Mr-Anyone commented Aug 9, 2025

Unreachable destructor of temporary object leads to confusing diagnostics messages.

This patch forwards the correct source location for better and less confusing diagnostics.

fixes #152477

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:analysis labels Aug 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 9, 2025

@llvm/pr-subscribers-clang

Author: Vincent (Mr-Anyone)

Changes

fixes #152477


Full diff: https://github.com/llvm/llvm-project/pull/152839.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Analysis/ReachableCode.cpp (+5)
  • (modified) clang/test/SemaCXX/warn-unreachable.cpp (+13)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 17e3df467593d..f3b21479c7470 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -178,6 +178,8 @@ Bug Fixes to C++ Support - Diagnose binding a reference to ``*nullptr`` during constant evaluation. (#GH48665) - Suppress ``-Wdeprecated-declarations`` in implicitly generated functions. (#GH147293) - Fix a crash when deleting a pointer to an incomplete array (#GH150359). +- Diagnosing the correct location for a function with non-POD data return-type being + labeled ``[[noreturn]]``. - Fix an assertion failure when expression in assumption attribute (``[[assume(expr)]]``) creates temporary objects. diff --git a/clang/lib/Analysis/ReachableCode.cpp b/clang/lib/Analysis/ReachableCode.cpp index 4a9ab5d9f0f73..c7de8836932a3 100644 --- a/clang/lib/Analysis/ReachableCode.cpp +++ b/clang/lib/Analysis/ReachableCode.cpp @@ -601,6 +601,11 @@ static SourceLocation GetUnreachableLoc(const Stmt *S, S = Ex->IgnoreParenImpCasts(); switch (S->getStmtClass()) { + case Expr::CXXBindTemporaryExprClass: { + const CXXBindTemporaryExpr *TemporaryExpr = cast<CXXBindTemporaryExpr>(S); + R1 = TemporaryExpr->getTemporary()->getDestructor()->getSourceRange(); + return R1.getBegin(); + } case Expr::BinaryOperatorClass: { const BinaryOperator *BO = cast<BinaryOperator>(S); return BO->getOperatorLoc(); diff --git a/clang/test/SemaCXX/warn-unreachable.cpp b/clang/test/SemaCXX/warn-unreachable.cpp index e6f5bc5ef8e12..ce574cfafa8e1 100644 --- a/clang/test/SemaCXX/warn-unreachable.cpp +++ b/clang/test/SemaCXX/warn-unreachable.cpp @@ -414,3 +414,16 @@ void tautological_compare(bool x, int y) { calledFun(); } + +namespace GH152477{ + class A{ + public: + ~A(); // expected-warning {{will never be executed}} + }; + + [[noreturn]] A never_return_so_destructor_never_called(); + + void func(){ + never_return_so_destructor_never_called(); + } +}; 
@llvmbot
Copy link
Member

llvmbot commented Aug 9, 2025

@llvm/pr-subscribers-clang-analysis

Author: Vincent (Mr-Anyone)

Changes

fixes #152477


Full diff: https://github.com/llvm/llvm-project/pull/152839.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Analysis/ReachableCode.cpp (+5)
  • (modified) clang/test/SemaCXX/warn-unreachable.cpp (+13)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 17e3df467593d..f3b21479c7470 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -178,6 +178,8 @@ Bug Fixes to C++ Support - Diagnose binding a reference to ``*nullptr`` during constant evaluation. (#GH48665) - Suppress ``-Wdeprecated-declarations`` in implicitly generated functions. (#GH147293) - Fix a crash when deleting a pointer to an incomplete array (#GH150359). +- Diagnosing the correct location for a function with non-POD data return-type being + labeled ``[[noreturn]]``. - Fix an assertion failure when expression in assumption attribute (``[[assume(expr)]]``) creates temporary objects. diff --git a/clang/lib/Analysis/ReachableCode.cpp b/clang/lib/Analysis/ReachableCode.cpp index 4a9ab5d9f0f73..c7de8836932a3 100644 --- a/clang/lib/Analysis/ReachableCode.cpp +++ b/clang/lib/Analysis/ReachableCode.cpp @@ -601,6 +601,11 @@ static SourceLocation GetUnreachableLoc(const Stmt *S, S = Ex->IgnoreParenImpCasts(); switch (S->getStmtClass()) { + case Expr::CXXBindTemporaryExprClass: { + const CXXBindTemporaryExpr *TemporaryExpr = cast<CXXBindTemporaryExpr>(S); + R1 = TemporaryExpr->getTemporary()->getDestructor()->getSourceRange(); + return R1.getBegin(); + } case Expr::BinaryOperatorClass: { const BinaryOperator *BO = cast<BinaryOperator>(S); return BO->getOperatorLoc(); diff --git a/clang/test/SemaCXX/warn-unreachable.cpp b/clang/test/SemaCXX/warn-unreachable.cpp index e6f5bc5ef8e12..ce574cfafa8e1 100644 --- a/clang/test/SemaCXX/warn-unreachable.cpp +++ b/clang/test/SemaCXX/warn-unreachable.cpp @@ -414,3 +414,16 @@ void tautological_compare(bool x, int y) { calledFun(); } + +namespace GH152477{ + class A{ + public: + ~A(); // expected-warning {{will never be executed}} + }; + + [[noreturn]] A never_return_so_destructor_never_called(); + + void func(){ + never_return_so_destructor_never_called(); + } +}; 
@github-actions
Copy link

github-actions bot commented Aug 9, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@Mr-Anyone Mr-Anyone force-pushed the fix_unreachable_code branch from ccfa6a7 to ee1bcce Compare August 9, 2025 07:19
@steakhal steakhal self-requested a review August 14, 2025 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:analysis clang Clang issues not falling into any other category

2 participants