Missing Return Statement in a Non-Void Method Compiles

Missing return statement in a non-void method compiles

Why would a language allow us to have a non-void method having an infinite loop and not returning anything?

The rule for non-void methods is every code path that returns must return a value, and that rule is satisfied in your program: zero out of zero code paths that return do return a value. The rule is not "every non-void method must have a code path that returns".

This enables you to write stub-methods like:

IEnumerator IEnumerable.GetEnumerator() 
{
throw new NotImplementedException();
}

That's a non-void method. It has to be a non-void method in order to satisfy the interface. But it seems silly to make this implementation illegal because it does not return anything.

That your method has an unreachable end point because of a goto (remember, a while(true) is just a more pleasant way to write goto) instead of a throw (which is another form of goto) is not relevant.

Why doesn't the compiler even warn about returning something?

Because the compiler has no good evidence that the code is wrong. Someone wrote while(true) and it seems likely that the person who did that knew what they were doing.

Where can I read more about reachability analysis in C#?

See my articles on the subject, here:

ATBG: de facto and de jure reachability

And you might also consider reading the C# specification.

What's the return value of a non-void function missing return statement?

It is an undefined behaviour. In many cases it appears like it is working, because the value to be returned is present in the expected place (like CPU register EAX on x86/x64), but this is a coincidence, nothing you could rely on.

Missing return statement compiler warning

If the method isn't a void method (like this one), all the possible branches of the method must return something. In this case, you aren't returning anything if an exception is thrown in the second try block.

You could just move the return cl statement to the end of the method instead of the end of the try block.

Missing return statement within if / for / while

If you put a return statement in the if, while or for statement then it may or may not return a value. If it will not go inside these statements then also that method should return some value (that could be null). To ensure that, compiler will force you to write this return statement which is after if, while or for.

But if you write an if / else block and each one of them is having a return in it then the compiler knows that either the if or else will get executed and the method will return a value. So this time the compiler will not force you.

if(condition)
{
return;
}
else
{
return;
}

Java missing return statement

If you have a method returning something other than void, you need to make sure that every logical path through that method explicitly returns something of the appropriate object/type. In your case, consider the situation where howManyTimes is less than or equal to 3 - it will skip the while block, and go to the end. Since there is no explicit return statement there, the method will not return anything in that situation, which is incorrect - it should return an int in all cases.

It should also be noted that the last statement in a non-void method should either be a return or a throw.

CUDA: missing return statement at end of non-void function in constexpr if function

Our compiler team looked at the issue. The warning is a spurious warning. We hope to address it in a future CUDA release. I won't be able to respond to questions about when that may be. The code is being generated correctly.

If you wish to silence the warning, a possible method is to add an additional return statement at the end of the function in question. This should have no effect in this case because it is unreachable:

template <typename T, Device D>
__host__ inline Pointer<T, D> AllocateSize(const size_t size) noexcept {
if constexpr (D == Device::CPU) {
return CPU_Ptr<T>(reinterpret_cast<T*>(std::malloc(size)));
} else {
T* p;
cudaMalloc(reinterpret_cast<void**>(&p), size);
return GPU_Ptr<T>(p);
}
return Pointer<T, D>(NULL);
}

(3028897)

How do I avoid getting Missing return statement when calling a method that throws an exception, from within another method?

Just swap around the terms, you'll never get to return if the method throws.

 if(!success){
callMethodThatAlwaysThrowsUncheckedExceptions();
}

return result;

Or even

 callMethodThatAlwaysThrowsUncheckedExceptions(succes);
return result;

Just check the success condition in your throwing method.

missing return statement even method is having void return type

Void is a reference type.

void is a language primitive.

You don't need a return statement when your return type is void.



Related Topics



Leave a reply



Submit