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

Wrong "destroyed here" location for temporaries

Open usx95 opened this issue 7 months ago • 2 comments

#include <iostream>
#include <vector>

void foo() {
    auto it = std::vector<int>{1, 2, 3}.begin();
    std::cout << *it;
}

Warnings:

<source>:5:15: warning: object whose reference is captured does not live long enough [-Wlifetime-safety-use-after-scope]
    5 |     auto it = std::vector<int>{1, 2, 3}.begin();
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~
<source>:5:39: note: destroyed here
    5 |     auto it = std::vector<int>{1, 2, 3}.begin();
      |                                       ^
<source>:6:19: note: later used here
    6 |     std::cout << *it;
      |                   ^~
1 warning generated.
Compiler returned: 0

The destruction of temporaries happen at the end of the statement (full-expression) and not at the end of expression which created it.

usx95 avatar May 03 '26 06:05 usx95

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

Author: Utkarsh Saxena (usx95)

```cpp #include <iostream> #include <vector>

void foo() { auto it = std::vector<int>{1, 2, 3}.begin(); std::cout << *it; }

Warnings:

<source>:5:15: warning: object whose reference is captured does not live long enough [-Wlifetime-safety-use-after-scope] 5 | auto it = std::vector<int>{1, 2, 3}.begin(); | ^~~~~~~~~~~~~~~~~~~~~~~~~ <source>:5:39: note: destroyed here 5 | auto it = std::vector<int>{1, 2, 3}.begin(); | ^ <source>:6:19: note: later used here 6 | std::cout << *it; | ^~ 1 warning generated. Compiler returned: 0

The destruction of temporaries happen at the end of the statement and not at the end of expression which created it.
</details>

I would like to work on this.

NeKon69 avatar May 29 '26 20:05 NeKon69