Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] Support functional cast in bugprone-dangling-handle #69067

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ 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(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.
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() {
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ 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, 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
`IgnoreMacros` to ignore warnings in macros.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down