Wrong "destroyed here" location for temporaries
#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.
@llvm/issue-subscribers-clang-temporal-safety
Author: Utkarsh Saxena (usx95)
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.