Skip to content

Commit 1097c71

Browse files
authored
[clang-tidy] Support functional cast in bugprone-dangling-handle (#69067)
Add support for constructor conversion based functional cast. Allows to detect issues like: const std::string_view test1 = std::string(a);
1 parent 9c3c0e3 commit 1097c71

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,20 @@ handleFrom(const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle,
3333

3434
ast_matchers::internal::Matcher<Stmt> handleFromTemporaryValue(
3535
const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
36+
37+
const auto TemporaryExpr = anyOf(
38+
cxxBindTemporaryExpr(),
39+
cxxFunctionalCastExpr(
40+
hasCastKind(CK_ConstructorConversion),
41+
hasSourceExpression(ignoringParenImpCasts(cxxBindTemporaryExpr()))));
3642
// If a ternary operator returns a temporary value, then both branches hold a
3743
// temporary value. If one of them is not a temporary then it must be copied
3844
// into one to satisfy the type of the operator.
3945
const auto TemporaryTernary = conditionalOperator(
40-
hasTrueExpression(ignoringParenImpCasts(cxxBindTemporaryExpr())),
41-
hasFalseExpression(ignoringParenImpCasts(cxxBindTemporaryExpr())));
46+
hasTrueExpression(ignoringParenImpCasts(TemporaryExpr)),
47+
hasFalseExpression(ignoringParenImpCasts(TemporaryExpr)));
4248

43-
return handleFrom(IsAHandle, anyOf(cxxBindTemporaryExpr(), TemporaryTernary));
49+
return handleFrom(IsAHandle, anyOf(TemporaryExpr, TemporaryTernary));
4450
}
4551

4652
ast_matchers::internal::Matcher<RecordDecl> isASequence() {

clang-tools-extra/docs/ReleaseNotes.rst

+5
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ New check aliases
207207
Changes in existing checks
208208
^^^^^^^^^^^^^^^^^^^^^^^^^^
209209

210+
- Improved :doc:`bugprone-dangling-handle
211+
<clang-tidy/checks/bugprone/dangling-handle>` check to support functional
212+
casting during type conversions at variable initialization, now with improved
213+
compatibility for C++17 and later versions.
214+
210215
- Improved :doc:`bugprone-lambda-function-name
211216
<clang-tidy/checks/bugprone/lambda-function-name>` check by adding option
212217
`IgnoreMacros` to ignore warnings in macros.

clang-tools-extra/test/clang-tidy/checkers/bugprone/dangling-handle.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ void Positives() {
108108
std::string_view view4(ReturnsAString());
109109
// CHECK-MESSAGES-CXX14: [[@LINE-1]]:20: warning: std::basic_string_view outlives
110110
// CHECK-MESSAGES-CXX17: [[@LINE-2]]:26: warning: std::basic_string_view outlives
111+
112+
std::string_view view5 = std::string("test");
113+
// CHECK-MESSAGES-CXX14: [[@LINE-1]]:20: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
114+
// CHECK-MESSAGES-CXX17: [[@LINE-2]]:28: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
115+
116+
std::string_view view6 = std::string{"test"};
117+
// CHECK-MESSAGES-CXX14: [[@LINE-1]]:20: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
118+
// CHECK-MESSAGES-CXX17: [[@LINE-2]]:28: warning: std::basic_string_view outlives its value [bugprone-dangling-handle]
111119
}
112120

113121
void OtherTypes() {

0 commit comments

Comments
 (0)