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

tx0/c: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 reads a value that was never returned, which is undefined.

``c 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 call that does not return, such as exit or abort, or in a function declared _Noreturn or __attribute__((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. Only return types written with keywords, or with a pointer, are known to have a value; a type name of a header might stand for void. main 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.

Example from the rule's tests
#include <stdlib.h> int sign(int x){  if (x > 0)    return 1;  if (x < 0)    return -1;}tx0 reports: sign can reach its end without returning a value int checked(int x){  if (x > 0)    return 1;  abort();} int forever(int x){  while (1) {    if (x > 3)      return x;    x++;  }} void nothing(int x){  if (x)    return;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

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

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