[LifetimeSafety] Detect assignments of std::pair of view
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
This requires inference + TU analysis
https://godbolt.org/z/ch4ETEzj8
Return stack addr is detected. Use-after-scope involving assignments is not being detected.
```cpp
#include
#include
#include
std::pair ReturnDanglingPair1() {
std::string local = "hello";
return {local, "world"}; // expected-error {{address of stack memory is returned later}}
}
std::pair ReturnDanglingPair2() {
std::string local = "hello";
return {"world", local}; // expected-error {{address of stack memory is returned later}}
}
std::pair ReturnDanglingPair3() {
std::string local1 = "hello";
std::string local2 = "world";
return {local1, local2}; // expected-error 2 {{address of stack memory is returned later}}
}
// FIXME: The compiler currently doesn't catch these UseAfterScope cases for pair.
void UseAfterScope1() {
std::pair p;
{
std::string local = "hello";
p = {local, "world"};
}
(void)p.first;
}
void UseAfterScope2() {
std::pair p;
{
std::string local = "hello";
p = {"world", local};
}
(void)p.second;
}
void UseAfterScope3() {
std::pair p;
{
std::string local = "hello";
p = {local, "world"};
}
(void)p.first;
(void)p.second;
}
```
Contributor guide
Assessment
This issue has not been assessed yet.