A function that returns a value can reach its end without a return statement

tx0/cpp:missing-return-valuerecommendedwarningreliability/control-flowdepth fastbug

What it finds

A function declared to return a value in which some path reaches the closing brace: the caller then uses a value that was never returned, which is undefined in C++, and compilers may assume that such a path is never taken.

``cpp int sign(int x) { if (x > 0) return 1; if (x < 0) return -1; } // sign(0) returns nothing ``

The rule follows each function through the shared IR, so a path that ends in a throw, in a call that does not return, such as std::abort or std::terminate, or in a function declared [[noreturn]] in the file or, with the translation-unit view, in its headers, does not count, nor does a path that the conditions on it rule out. A function that calls one declared [[noreturn]] inside a namespace or class is left alone, and so is one whose last statement leaves open whether the end is reached: a switch without default whose cases cover the values the author expects, often every enumerator, an if constexpr, or a call of a function that is no member and may not return, as one that throws. Only some return types are known to hold a value: types written with keywords other than void, pointers and references, types of the standard library written with std:: and no template arguments, a few class templates of it such as std::vector, and classes and enums the file defines. Aliases such as std::enable_if_t<...> may stand for void. auto and names of other headers, which may stand for void, are left alone, and so is main, which returns 0 at its end. In the file as written, a function that calls something neither the file nor the C library declares is left alone, since it may be a macro that returns or a function that does not.

From the rule's source, missing_return_value.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:missing-return-value" = "warning"

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:missing-return-value] reason="..."

tx0 explain tx0/cpp:missing-return-value shows the same in the terminal. Found a false positive? Report it.