A function that returns a reference returns a local variable or by-value parameter, which is destroyed when the function returns

tx0/cpp:reference-to-local-returnedrecommendederrorreliability/memory-safetydepth fastbug, cwe-562

What it finds

const std::string &name() { std::string s = ...; return s; } returns a reference to an object that dies at the end of the function; the caller reads freed stack memory. The function returns by value instead, or returns an object that outlives the call.

From the rule's source, reference_to_local_returned.rs.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/cpp:reference-to-local-returned" = "error"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/cpp:reference-to-local-returned] reason="..."

tx0 explain tx0/cpp:reference-to-local-returned shows the same in the terminal. Found a false positive? Report it.