-Wdangling-field detects view members initalized from a by-value owner parameter
struct Foo {};
struct Bar {
const Foo& foo;
Bar(Foo f ) : foo(f) {} // good, clang warn on this case
};
struct C {
string_view s;
C(std::string c) : s(c) {} // bad, clang doesn't warn on this case. support it
};
@llvm/issue-subscribers-clang-temporal-safety
Author: Haojian Wu (hokein)
struct C { string_view s; C(std::string c) : s(c) {} // bad, clang doesn't warn on this case. support it };
</details>
A potential implementation
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -1288,8 +1288,15 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
// Suppress false positives for code like the one below:
// Ctor(unique_ptr<T> up) : pointer(up.get()), owner(move(up)) {}
// FIXME: move this logic to analyzePathForGSLPointer.
- if (DRE && isGslOwnerType(DRE->getType()))
- return false;
+ if (DRE && isGslOwnerType(DRE->getType())) {
+ if (const auto *Member =
+ ExtendingEntity ? ExtendingEntity->getDecl() : nullptr;
+ Member && isGslPointerType(Member->getType())) {
+ // Do not suppress if member is a GSL pointer!
+ } else {
+ return false;
+ }
+ }
The new -Wlifetime-safety diagnostic (which will be enabled soon) already covers this case. I think there is no harm in detecting it in -Wdangling-field as well, though it is probably low priority.
The new -Wlifetime-safety diagnostic (which will be enabled soon) already covers this case.
Yes. It covers both the cases mentioned. https://godbolt.org/z/7W7a3dE6r
I think there is no harm in detecting it in -Wdangling-field as well, though it is probably low priority.
Yes. I would treat this as a lower priority. I do not have plans to work on -Wdangling given that it would be replaced by -Wlifetime-safety
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
- Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: Haojian Wu (hokein)
struct C { string_view s; C(std::string c) : s(c) {} // bad, clang doesn't warn on this case. support it };
</details>
Is this still relevant to work on even if Wlifetime-safety might replace it? If so can I get assigned?
I’d like to work on this issue. I’ll start investigating and submit a patch.
Hi, I would like to work on this issue can it be assigned to me?