[clang-tools-extra] [clang-tidy] Support functional cast in bugprone-dangling-handle (PR #69067)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 25 12:38:46 PDT 2023


https://github.com/PiotrZSL updated https://github.com/llvm/llvm-project/pull/69067

>From 6d4c7297e19193781d97fe19c3ccf8e65226ad0b Mon Sep 17 00:00:00 2001
From: Piotr Zegar <me at piotrzegar.pl>
Date: Sat, 14 Oct 2023 14:51:16 +0000
Subject: [PATCH 1/3] [clang-tidy] Support functional cast in
 bugprone-dangling-handle

Add support for constructor conversion based functional
cast. Allows to detect issues like:
const std::string_view test1 = std::string(a);
---
 .../clang-tidy/bugprone/DanglingHandleCheck.cpp       | 11 ++++++++---
 clang-tools-extra/docs/ReleaseNotes.rst               |  4 ++++
 .../clang-tidy/checkers/bugprone/dangling-handle.cpp  |  8 ++++++++
 3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp
index 9ded699ba78e66b..ffaa1edc83e7299 100644
--- a/clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp
@@ -33,14 +33,19 @@ handleFrom(const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle,
 
 ast_matchers::internal::Matcher<Stmt> handleFromTemporaryValue(
     const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
+
+  const auto TemporaryExpr =
+      anyOf(cxxBindTemporaryExpr(),
+            cxxFunctionalCastExpr(hasCastKind(CK_ConstructorConversion),
+                                  hasSourceExpression(cxxBindTemporaryExpr())));
   // If a ternary operator returns a temporary value, then both branches hold a
   // temporary value. If one of them is not a temporary then it must be copied
   // into one to satisfy the type of the operator.
   const auto TemporaryTernary = conditionalOperator(
-      hasTrueExpression(ignoringParenImpCasts(cxxBindTemporaryExpr())),
-      hasFalseExpression(ignoringParenImpCasts(cxxBindTemporaryExpr())));
+      hasTrueExpression(ignoringParenImpCasts(TemporaryExpr)),
+      hasFalseExpression(ignoringParenImpCasts(TemporaryExpr)));
 
-  return handleFrom(IsAHandle, anyOf(cxxBindTemporaryExpr(), TemporaryTernary));
+  return handleFrom(IsAHandle, anyOf(TemporaryExpr, TemporaryTernary));
 }
 
 ast_matchers::internal::Matcher<RecordDecl> isASequence() {
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 03e5dc6f164af2a..da4f55da65ad51f 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -190,6 +190,10 @@ New check aliases
 Changes in existing checks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Improved :doc:`bugprone-dangling-handle
+  <clang-tidy/checks/bugprone/dangling-handle>` check to support functional
+  casting during type conversions at variable initialization.
+
 - Improved :doc:`bugprone-lambda-function-name
   <clang-tidy/checks/bugprone/lambda-function-name>` check by adding option
   `IgnoreMacros` to ignore warnings in macros.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/dangling-handle.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/dangling-handle.cpp
index 23cda5321764383..96c812617038a37 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/dangling-handle.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/dangling-handle.cpp
@@ -108,6 +108,14 @@ void Positives() {
   std::string_view view4(ReturnsAString());
   // CHECK-MESSAGES-CXX14: [[@LINE-1]]:20: warning: std::basic_string_view outlives
   // CHECK-MESSAGES-CXX17: [[@LINE-2]]:26: warning: std::basic_string_view outlives
+
+  std::string_view view5 = std::string("test");
+  // CHECK-MESSAGES-CXX14: [[@LINE-1]]:20: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
+  // CHECK-MESSAGES-CXX17: [[@LINE-2]]:28: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
+
+  std::string_view view6 = std::string{"test"};
+  // CHECK-MESSAGES-CXX14: [[@LINE-1]]:20: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
+  // CHECK-MESSAGES-CXX17: [[@LINE-2]]:28: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
 }
 
 void OtherTypes() {

>From 8591cdb8288775ac660c2f3d9e2fa4ee2c30bed2 Mon Sep 17 00:00:00 2001
From: Piotr Zegar <me at piotrzegar.pl>
Date: Sat, 14 Oct 2023 17:13:32 +0000
Subject: [PATCH 2/3] [clang-tidy][DOC] Improve release notes for
 bugprone-dangling-handle

Add info about change that were introduced in
commit f2e5000937235aa35a9ee4423045b265c2c79e85.
---
 clang-tools-extra/docs/ReleaseNotes.rst | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index da4f55da65ad51f..35d02f7c42f82fd 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -192,7 +192,8 @@ Changes in existing checks
 
 - Improved :doc:`bugprone-dangling-handle
   <clang-tidy/checks/bugprone/dangling-handle>` check to support functional
-  casting during type conversions at variable initialization.
+  casting during type conversions at variable initialization, now with improved
+  compatibility for C++17 and later versions.
 
 - Improved :doc:`bugprone-lambda-function-name
   <clang-tidy/checks/bugprone/lambda-function-name>` check by adding option

>From b2df7383a76aea456e9eb5a44dde9db786e22fb5 Mon Sep 17 00:00:00 2001
From: Piotr Zegar <me at piotrzegar.pl>
Date: Wed, 25 Oct 2023 19:36:40 +0000
Subject: [PATCH 3/3] Add ignorePareImpCast

---
 .../clang-tidy/bugprone/DanglingHandleCheck.cpp          | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp
index ffaa1edc83e7299..d55df3a6d7b741b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp
@@ -34,10 +34,11 @@ handleFrom(const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle,
 ast_matchers::internal::Matcher<Stmt> handleFromTemporaryValue(
     const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
 
-  const auto TemporaryExpr =
-      anyOf(cxxBindTemporaryExpr(),
-            cxxFunctionalCastExpr(hasCastKind(CK_ConstructorConversion),
-                                  hasSourceExpression(cxxBindTemporaryExpr())));
+  const auto TemporaryExpr = anyOf(
+      cxxBindTemporaryExpr(),
+      cxxFunctionalCastExpr(
+          hasCastKind(CK_ConstructorConversion),
+          hasSourceExpression(ignoringParenImpCasts(cxxBindTemporaryExpr()))));
   // If a ternary operator returns a temporary value, then both branches hold a
   // temporary value. If one of them is not a temporary then it must be copied
   // into one to satisfy the type of the operator.



More information about the cfe-commits mailing list