-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[clang-tidy] Support functional cast in bugprone-dangling-handle #69067
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
Merged
PiotrZSL
merged 3 commits into
llvm:main
from
PiotrZSL:68637-clang-tidy-bugprone-dangling-handle-works-with-fooa-but-not-fooa
Oct 26, 2023
Merged
[clang-tidy] Support functional cast in bugprone-dangling-handle #69067
PiotrZSL
merged 3 commits into
llvm:main
from
PiotrZSL:68637-clang-tidy-bugprone-dangling-handle-works-with-fooa-but-not-fooa
Oct 26, 2023
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add support for constructor conversion based functional cast. Allows to detect issues like: const std::string_view test1 = std::string(a);
Add info about change that were introduced in commit f2e5000.
@llvm/pr-subscribers-clang-tidy Author: Piotr Zegar (PiotrZSL) ChangesAdd support for constructor conversion based functional Full diff: https://github.com/llvm/llvm-project/pull/69067.diff 3 Files Affected:
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..35d02f7c42f82fd 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -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.
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() {
|
HerrCai0907
reviewed
Oct 21, 2023
HerrCai0907
approved these changes
Oct 26, 2023
zahiraam
pushed a commit
to zahiraam/llvm-project
that referenced
this pull request
Oct 26, 2023
…m#69067) Add support for constructor conversion based functional cast. Allows to detect issues like: const std::string_view test1 = std::string(a);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add support for constructor conversion based functional
cast. Allows to detect issues like:
const std::string_view test1 = std::string(a);