简体   繁体   中英

Why do I get the warning 'control reaches end of non-void function' even when the return is guaranteed

I understand that, given the code never enters the loop, would give me the error. However, given that r is equal to 1 , it should return it, ie , it would always enter it.

int arroz() {

    int r = 1;
    while (r) {
        return r;
    }
}

So, why does this code give me the warning control reaches end of non-void function?

The compiler correctly assumes that r is a variable value, it does not process it as a constant, that being the case it cannot be sure what the value is going to be and therefore if the body of the cycle will or will not be executed.

If you use a constant the warning goes away because the compiler will assemble the code using a constant value.

You can check this behavior here .

IMO it is a bug in the compiler. The analyzer should see that there is no execution path avoiding the return statement. The variable is not volatile .

The code is compiled to:

arroz:                                  # @arroz
        mov     eax, 1
        ret

This shows that compiler know that it will always return 1

You should report the bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM