llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

-Wdangling-field detects view members initalized from a by-value owner parameter

Open hokein opened this issue 8 months ago • 8 comments

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
};

hokein avatar Mar 20 '26 11:03 hokein

@llvm/issue-subscribers-clang-temporal-safety

Author: Haojian Wu (hokein)

``` 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 };

</details>

llvmbot avatar Mar 20 '26 11:03 llvmbot

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.

hokein avatar Mar 20 '26 11:03 hokein

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

usx95 avatar Mar 20 '26 11:03 usx95

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:

  1. 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.
  2. 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.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. 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.

llvmbot avatar Mar 21 '26 06:03 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: Haojian Wu (hokein)

``` 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 };

</details>

llvmbot avatar Mar 21 '26 06:03 llvmbot

Is this still relevant to work on even if Wlifetime-safety might replace it? If so can I get assigned?

pau-sum avatar Mar 21 '26 19:03 pau-sum

I’d like to work on this issue. I’ll start investigating and submit a patch.

david04g avatar May 03 '26 18:05 david04g

Hi, I would like to work on this issue can it be assigned to me?

Ippo47 avatar May 05 '26 22:05 Ippo47