llvm / llvm/llvm-project

Returning the value of an `optional<T&>` parameter from a function triggers incorrect `-Wreturn-stack-address`

Open
#205,644 1 comment 0 reactions 0 assignees View on GitHub
clang:diagnostics
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```c++
#include

int& test(std::optional optional) {
return *optional;
}

int main() {
int value = 42;
return test(value);
}
```

The bug happens with version 22. It works fine on trunk.

[Link to compiler explorer](https://compiler-explorer.com/z/nh3Y9aaaj)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the C++ snippet in Compiler Explorer with version 22 and trunk, comparing the -Wreturn-stack-address diagnostics. Trace the compiler warning path for returning *optional and verify that the valid reference return no longer triggers the incorrect warning, matching trunk behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.